import { gameConfigHandle } from "core/config/gameConfig"
import gameUtils from "core/utils/gameUtils"
import { GameRouteData, GameTime, localApi } from "core/utils/localApi"

import utils from "core/utils/utils"
import adventureHandle from "core/handle/adventureHandle"
import bagHandle from "core/handle/bagHandle"
import equipmentHandle from "core/handle/equipmentHandel"
import itemHandle from "core/handle/itemHandle"
import marketHandle from "core/handle/marketHandle"
import partnerHandle from "core/handle/partnerHandle"
import playerHandle from "core/handle/playerHandle"
import skillHandle from "core/handle/skillHandle"
import talentHandle from "core/handle/talentHandle"
import { BagModel } from "core/model/bagModel"
import { AttriItemModel, ItemModel } from "core/model/itemModel"
import { BagType, AttriType, EquipmentType, OccType } from "core/typeDefine/typeDefine"
import { TreasureItemModel } from "core/model/treasureModel"
import petHandle from "core/handle/petHandle"
import { EnchantModel, EquipmentsEnchantsModel, EquipmentsLvlupModel, PartnerModel } from "core/model/partnerModel"
import luckHandle from "core/handle/luckHandle"
import { PackageModel, PackagesModel } from "core/model/equipmentsModel"
import spiritHandle from "core/handle/spiritHandle"


class NormalRoute {
    @localApi("normalRoute")
    refreshSpiritTotalAttris(occType: OccType, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.refreshSpiritTotalAttris
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let spiritConfig = gameConfigHandle.gameConfig.spirit
        player.spirit.totalLvlAttris[occType] = utils.getRandomArrayNoRepeat(spiritConfig.spiritTotalLvlAttris[occType], 2, random)
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    spiritLvlUp(id: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let lvl = player.spirit.spiritLvls[id] || 0
        let spiritConfig = gameConfigHandle.gameConfig.spirit
        let lvlMax = spiritConfig.maxLvl
        if (lvl >= lvlMax) {
            return { err: 2 }
        }
        let occType = gameConfigHandle.configByID[id].occType
        let needConfig = gameConfigHandle.gameConfig.need.spiritLvlup[occType]
        let res = gameUtils.useNeedResource(player, needConfig, lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.spirit.spiritLvls[id] = lvl + 1
        if (!player.spirit.totalLvlAttris[occType]) {
            player.spirit.totalLvlAttris[occType] = utils.getRandomArrayNoRepeat(spiritConfig.spiritTotalLvlAttris[occType], 2, random)
        }
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    changeSpiritItem(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let id = player.spirit.changeItems[index]
        if (bagHandle.getTotalNumByItemId(player.bag, id) < 1) {
            return { err: 1 }
        }
        bagHandle.removeItemByItemId(player.bag, id, 1)
        bagHandle.addItem(player.bag, gameConfigHandle.configByID[id].changeId, 1)
        player.spirit.changeItems.splice(index, 1)
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    refreshSpiritChangeItems(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.refreshSpiritChangeItems
        let res = gameUtils.useNeedResource(player, needConfig, player.spirit.refreshTimes)
        if (res !== 0) {
            return { err: 1 }
        }
        player.spirit.refreshTimes++
        spiritHandle.refreshChangeItems(player, random)
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    selectDisciple(friendshipId: number, heroId: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let friendship = player.friendship
        let lvl = friendship.friendshipLvls[friendshipId] || 0
        let unlockLvl = gameConfigHandle.gameConfig.friendship.unlockSkillLvl[0]
        if (lvl < unlockLvl) {
            return { err: 100 }
        }
        let heroOccType = gameConfigHandle.configByID[heroId].occType
        let friendshipOccType = gameConfigHandle.configByID[friendshipId].occType
        if (heroOccType !== friendshipOccType) {
            return { err: 1 }
        }
        let disciples = Object.entries(friendship.disciples)
        for (let i = 0; i < disciples.length; i++) {
            let oldId = disciples[i][1]
            if (oldId as number == heroId) {
                return { err: 2 }
            }
        }
        let needConfig = gameConfigHandle.gameConfig.need.choseDisciple
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 3 }
        }
        friendship.disciples[friendshipId] = heroId
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    friendshipSkillLvlUp(id: number, type: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let index = player.friendship.friendshipSkills[id] || 0
        if (index >= 3) {
            return { err: 100 }
        }
        let unlockSkillLvl = gameConfigHandle.gameConfig.friendship.unlockSkillLvl[index + 1]
        let lvl = player.friendship.friendshipLvls[id] || 0
        if (lvl < unlockSkillLvl) {
            return { err: 2 }
        }
        let occType = gameConfigHandle.configByID[id].occType
        let needConfig = gameConfigHandle.gameConfig.need.friendshipSkillUp[occType]
        let res = gameUtils.useNeedResource(player, needConfig, index + type)
        if (res !== 0) {
            return { err: 1 }
        }
        player.friendship.friendshipSkills[id] = index + 1
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    changeFriendshipSubattri(id: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.resetFriendshipSubattri
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        // let subAmount = playerHandle.getFriendshipSubAmount(player, id)
        player.friendship.subAttris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].subAttris, 1, random)
        player.friendship.attris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].attris, 2, random)
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    friendshipLvlUp(id: number, type: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let lvl = player.friendship.friendshipLvls[id] || 0
        let lvlMax = gameConfigHandle.gameConfig.friendship.maxLvl
        if (lvl >= lvlMax) {
            return { err: 1 }
        }
        let needConfig = gameConfigHandle.gameConfig.need.friendshipLvlUp[type]
        let res = gameUtils.useNeedResource(player, needConfig, lvl)
        if (res !== 0) {
            return { err: 2 }
        }
        player.friendship.friendshipLvls[id] = lvl + 1
        if (!player.friendship.subAttris[id]) {
            player.friendship.subAttris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].subAttris, 1, random)
            player.friendship.attris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].attris, 2, random)
        }
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    buyItemInTrade(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let haveTimes = luckHandle.getTradeTimes(player, playerHandle.getVip(player).vipLvl, isMonthVip).haveTimes
        if (haveTimes < 1) {
            return { err: 1 }
        }
        let trade = player.luck.trade
        let needConfig = gameConfigHandle.gameConfig.need.tradeBuyGoods[trade.tradeItems[index].useItem]
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 2 }
        }
        trade.times++
        let getItems = []
        trade.exp += 1
        getItems.push({ id: 'tradeExp', amount: 1 })
        let luckConfig = gameConfigHandle.gameConfig.luck
        let addPer = utils.getIncValues(luckConfig.tradeLvlGetPer, trade.lvl)
        let lvlUpExp = utils.getIncValues(luckConfig.tradeLvlUpNeedExp, trade.lvl)
        if (trade.exp >= lvlUpExp && trade.lvl < luckConfig.tradeLvlMax) {
            trade.exp -= lvlUpExp
            trade.lvl++
        }
        let getItem = luckConfig.tradeGetItems[trade.tradeItems[index].getItem]
        let amount = Math.floor(getItem.amount * (1 + addPer / 100))
        if (typeof getItem.id == 'string') {
            if (getItem.id == 'gold') {
                let gold = Math.floor(adventureHandle.getGoldResoureOriginAdd(player) * getItem.amount * (1 + addPer / 100))
                player.itemInfo[getItem.id] += gold
                getItems.push({ id: getItem.id, amount: gold })
            } else {
                player.itemInfo[getItem.id] += amount
                getItems.push({ id: getItem.id, amount })
            }
        } else {
            getItems.push({ id: getItem.id, amount })
            bagHandle.addItem(player.bag, getItem.id, amount)
        }
        trade.tradeItems.splice(index, 1)
        if (trade.tradeItems.length < 1) {
            luckHandle.resetTradeGoods(player, random)
        }
        return {
            err: 0, data: getItems
        }
    }
    @localApi("normalRoute")
    buyTradeTimes(amount: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let canBuyTimes = luckHandle.getTradeTimes(player, playerHandle.getVip(player).vipLvl, isMonthVip).canBuyTimes
        if (canBuyTimes < 1) {
            return { err: 1 }
        }
        if (amount > canBuyTimes) {
            amount = canBuyTimes
        }
        let needConfig = gameConfigHandle.gameConfig.need.buyTradeTimes
        let res = gameUtils.useNeedResource(player, needConfig, amount)
        if (res !== 0) {
            return { err: 2 }
        }
        player.luck.trade.buyTimes += amount
        return {
            err: 0, data: amount
        }
    }
    @localApi("normalRoute")
    refreshTradeGoods(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.refreshTradeGoods
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        luckHandle.resetTradeGoods(player, random)
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    resetTradeGoods(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        luckHandle.resetTradeGoods(player, random)
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    backFrontLvl(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.frontBackLvl
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let front = player.luck.front
        if (front.lvl < front.lvlMax || front.lvl == 1) {
            return { err: 100 }
        }
        front.lvl--
        luckHandle.resetFrontItems(player, random)
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    buyItemUseFrontScore(amount: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.frontBuy
        let res = gameUtils.useNeedResource(player, needConfig, amount)
        if (res !== 0) {
            return { err: 1 }
        }
        let getItems = {}
        let buyItemsUseFrontScore = gameConfigHandle.gameConfig.luck.buyItemsUseFrontScore
        for (let i = 0; i < amount; i++) {
            let id: number = utils.getOneFromArray(buyItemsUseFrontScore, random)
            bagHandle.addItem(player.bag, id, 1)
            getItems[id] = (getItems[id] || 0) + 1
        }
        return {
            err: 0, data: getItems
        }
    }
    @localApi("normalRoute")
    toNextLvlOfFront(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let front = player.luck.front
        let isOk = luckHandle.isFrontExploreComplete(player)
        if (!isOk) {
            return { err: 100 }
        }
        let lvlMax = gameConfigHandle.gameConfig.luck.frontLvlMax
        if (front.lvl < lvlMax) {
            front.lvl++
        }
        if (front.lvlMax < front.lvl) {
            front.lvlMax = front.lvl
        }
        luckHandle.resetFrontItems(player, random)
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    exploreFront(i: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let front = player.luck.front
        if (front.openIndexs.includes(i)) {
            return { err: 100 }
        }
        let remain = luckHandle.getExplorePower(player, playerHandle.getVip(player).vipLvl, isMonthVip).remain
        if (remain < 1) {
            return { err: 1 }
        }
        front.times++
        front.openIndexs.push(i)
        if (front.items[i] == 4) {
            player.itemInfo.diamond += 10
        }
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    setFrontTeamFirstTime(type: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        if (player.team.frontTeamList.length >= 1 && type !== 1) {
            return { err: 100 }
        }
        player.team.frontTeamList = player.team.teamList.slice(0)
        // player.team.frontRecruitList = Object.assign({}, player.team.recruitList)
        player.team.frontRecruitList = JSON.parse(JSON.stringify(player.team.recruitList))
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    resetFrontItems(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        luckHandle.resetFrontItems(player, random)
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    getDayFrontScore(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let front = player.luck.front
        if (front.haveGetScore) {
            return { err: 1 }
        }
        let canGetScore = utils.getIncValues(gameConfigHandle.gameConfig.luck.frontScore, front.lvl)
        player.itemInfo.frontScore += canGetScore
        front.haveGetScore = true
        return {
            err: 0, data: canGetScore
        }
    }
    @localApi("normalRoute")
    unlockStarSkill(parnterId: number, index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, parnterId)
        let starLvl = partner.starLvl
        if (partner.starSkillUnlock.includes(index)) {
            return { err: 100 }
        }
        let unlockStarLvl = gameConfigHandle.gameConfig.partner.starSkillUnlockStarLvl[index]
        if (starLvl < unlockStarLvl) {
            return { err: 1 }
        }
        let occType = gameConfigHandle.configByID[parnterId].occType
        let needConfig = gameConfigHandle.gameConfig.need.starSkillUnlock[occType]
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 2 }
        }
        partner.starSkillUnlock.push(index)
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    getStartWorshipPoint(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        if (player.luck.worship.getFirstPoint) {
            return { err: 1 }
        }
        player.luck.worship.getFirstPoint = true
        player.itemInfo.worship = utils.getIncValues(gameConfigHandle.gameConfig.luck.worshipGetPoint, 0)
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    refreshPackage(occType: OccType, planIndex: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let packages: PackagesModel = player.teamEquipment.packageList[occType][planIndex]
        let lockAmount = 0
        gameConfigHandle.gameConfig.equipment.equipmentListType.forEach(type => {
            if (packages[type].lock) {
                lockAmount++
            }
        })
        if (lockAmount >= 7) {
            return { err: 2 }
        }
        let res = gameUtils.useNeedResource(player, gameConfigHandle.gameConfig.need.refreshPackages, lockAmount)
        if (res !== 0) {
            return { err: 1 }
        }
        gameConfigHandle.gameConfig.equipment.equipmentListType.forEach(type => {
            if (!packages[type].lock) {
                let ids = gameConfigHandle.gameConfig.equipment.packageAttributesList.concat()
                if (packages[type].id) {
                    ids.splice(ids.indexOf(packages[type].id), 1)
                }
                packages[type].id = utils.getOneFromArray(ids, random)
            }
        })
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    lockPackage(occType: OccType, planIndex: number, equipmentType: EquipmentType, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let packages: PackagesModel = player.teamEquipment.packageList[occType][planIndex]
        let lockAmount = 0
        gameConfigHandle.gameConfig.equipment.equipmentListType.forEach(type => {
            if (packages[type].lock) {
                lockAmount++
            }
        })
        let packageItem: PackageModel = player.teamEquipment.packageList[occType][planIndex][equipmentType]
        if (!packageItem.id) {
            return { err: 1 }
        }
        if (!packageItem.lock && lockAmount >= gameConfigHandle.gameConfig.equipment.packageLockMax) {
            return { err: 2 }
        }
        packageItem.lock = !packageItem.lock
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    setPackagePlanActive(occType: OccType, index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        player.teamEquipment.packageActive[occType] = index
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    useWorshipPoint(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let res = gameUtils.useNeedResource(player, gameConfigHandle.gameConfig.need.buyPackageProp, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let propId = utils.getOneFromArray(gameConfigHandle.gameConfig.equipment.packagePropList, random)
        bagHandle.addItem(player.bag, propId as number, 1)
        return {
            err: 0, data: propId
        }
    }
    @localApi("normalRoute")
    worshipLvlUp(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let worship = player.luck.worship
        let luckConfig = gameConfigHandle.gameConfig.luck
        if (worship.lvl >= luckConfig.worshipLvlMax) {
            return { err: 2 }
        }
        let upNeedExp = utils.getIncValues(luckConfig.worshipLvlUpNeedExp, worship.lvl)
        if (worship.exp < upNeedExp) {
            return { err: 1 }
        }
        worship.exp -= upNeedExp
        worship.lvl++
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    worshiping(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let haveTimes = luckHandle.getWortshipTimes(player, playerHandle.getVip(player).vipLvl, isMonthVip).haveTimes
        if (haveTimes < 1) {
            return { err: 1 }
        }
        let worship = player.luck.worship
        worship.times++
        let luckConfig = gameConfigHandle.gameConfig.luck
        let getItems = {}
        let getPoint = 1
        player.itemInfo.worship = (player.itemInfo.worship || 0) + getPoint
        getItems['worship'] = getPoint
        let rate = luckConfig.worshipRate
        let propId: number = 0
        if (random.intn(100) <= rate) {
            propId = utils.getOneFromArray(luckConfig.worshipGetProps[index], random)
        } else {
            propId = utils.getOneFromArray(gameConfigHandle.gameConfig.equipment.packagePropList, random)
        }
        bagHandle.addItem(player.bag, propId, 1)
        getItems[propId] = 1

        let getExp = utils.getIncValues(luckConfig.worshipGetExp, worship.lvl)
        getItems['worshipExp'] = getExp
        worship.exp += getExp

        let upNeedExp = utils.getIncValues(luckConfig.worshipLvlUpNeedExp, worship.lvl)
        while (worship.exp >= upNeedExp && worship.lvl < luckConfig.worshipLvlMax) {
            worship.exp -= upNeedExp
            worship.lvl++
            upNeedExp = utils.getIncValues(luckConfig.worshipLvlUpNeedExp, worship.lvl)
        }
        return {
            err: 0, data: getItems
        }
    }
    @localApi("normalRoute")
    buyWorshipTimes(amount: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let canBuyTimes = luckHandle.getWortshipTimes(player, playerHandle.getVip(player).vipLvl, isMonthVip).canBuyTimes
        if (canBuyTimes < 1) {
            return { err: 1 }
        }
        if (amount > canBuyTimes) {
            amount = canBuyTimes
        }
        let needConfig = gameConfigHandle.gameConfig.need.buyWorshipTimes
        let res = gameUtils.useNeedResource(player, needConfig, amount)
        if (res !== 0) {
            return { err: 2 }
        }
        player.luck.worship.buyTimes += amount
        return {
            err: 0, data: amount
        }
    }
    @localApi("normalRoute")
    addExplorePower(amount: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let canAddAmount = luckHandle.getExplorePower(player, playerHandle.getVip(player).vipLvl, isMonthVip).canBuy
        if (canAddAmount < 1) {
            return { err: 1 }
        }
        if (amount > canAddAmount) {
            amount = canAddAmount
        }
        let needConfig = gameConfigHandle.gameConfig.need.addExplorePower
        let res = gameUtils.useNeedResource(player, needConfig, amount)
        if (res !== 0) {
            return { err: 2 }
        }
        player.luck.front.buyTimes += amount
        return {
            err: 0, data: amount
        }
    }
    @localApi("normalRoute")
    addActionPower(amount: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let canAddAmount = gameConfigHandle.gameConfig.vip.vipAdd.buyActionPowerMax[playerHandle.getVip(player).vipLvl]
        canAddAmount -= player.luck.maze.buyActionPower
        if (canAddAmount < 1) {
            return { err: 1 }
        }
        if (amount > canAddAmount) {
            amount = canAddAmount
        }
        let needConfig = gameConfigHandle.gameConfig.need.addActionPower
        let res = gameUtils.useNeedResource(player, needConfig, amount)
        if (res !== 0) {
            return { err: 2 }
        }
        player.luck.maze.buyActionPower += amount
        return {
            err: 0, data: amount
        }
    }
    @localApi("normalRoute")
    refreshMazeMaps(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        if (!player.luck.maze.done) {
            return { err: 100 }
        }
        luckHandle.refreshMazeMaps(player, random)
        return {
            err: 0,
        }
    }

    @localApi("normalRoute")
    passMazeMap(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let maze = player.luck.maze
        if (!maze.mapState[index]) {
            return { err: 100 }//地图未探索
        }
        if (index == 0) {
            return { err: 100 }
        }
        let mapId = maze.maps[index]
        let mapData = gameConfigHandle.gameConfig.luck.mapData[mapId]
        let cost = mapData.cost
        if (cost) {
            if (cost.id == 'diamond') {
                if (player.itemInfo.diamond < cost.amount) {
                    return { err: 1 }
                }
                player.itemInfo.diamond -= cost.amount
            } else if (cost.id == 'actionPower') {
                let remainPower = luckHandle.getActionPower(player, isMonthVip).remain
                if (remainPower < cost.amount) {
                    return { err: 2 }
                }
                maze.actionPower += cost.amount
            }
        }
        let getList = []
        if (mapData.get) {
            let getNum = mapData.getNum || 1
            let getItems = utils.getRandomArrayNoRepeat(mapData.get, getNum, random)
            for (let i = 0; i < getItems.length; i++) {
                let itemConfig: any = getItems[i]
                let id = itemConfig['id']
                let amount = itemConfig['amount']
                if (typeof id == 'number') {
                    bagHandle.addItem(player.bag, id, amount)
                    getList.push({ id, amount })
                } else if (id == 'actionPower') {
                    maze.addActionPower += amount
                    getList.push({ id, amount })
                } else if (id == 'gold') {
                    let gold = adventureHandle.getGoldResoureOriginAdd(player) * amount
                    player.itemInfo.gold += gold
                    getList.push({ id, amount: gold })
                } else if (id == 'equipment') {
                    let equipment = equipmentHandle.create(player, 'maze', random, { minQuality: itemConfig.minQuality })
                    let res = bagHandle.addItemComponent(player.equipmentBag, equipment)
                    if (!res) {
                        return { err: 3 }//装备背包已满
                    }
                    getList.push({ id, amount: equipment.equipment.quality })
                }
            }
        }
        if (mapId == 6) {
            maze.done = true//开启宝藏 完成该地图探险
        }
        maze.maps[index] = 3//通过后地图变成道路
        return {
            err: 0, data: getList
        }
    }
    //寻宝活动探索地图
    @localApi("normalRoute")
    clickMazeMap(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let maze = player.luck.maze
        if (maze.mapState[index]) {
            return { err: 100 }
        }
        if (!luckHandle.canReachMazeMap(player, index)) {
            return { err: 2 }//不可抵达
        }
        let remain = luckHandle.getActionPower(player, isMonthVip).remain
        if (remain < 1) {
            return { err: 1 }
        }
        maze.actionPower++
        maze.mapState[index] = true
        return {
            err: 0,
        }
    }
    //寻宝活动初始地图刷新
    @localApi("normalRoute")
    setMazeMapsFirstTime(gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        luckHandle.resetMazeMaps(player, random)
        return {
            err: 0,
        }
    }
    //初始刷新卡牌
    @localApi("normalRoute")
    setCardFirstTime(gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let card = player.luck.card
        if (card.cards.length > 0) {
            return { err: 100 }
        }
        luckHandle.resetCard(player, random)
        return {
            err: 0,
        }
    }
    @localApi("normalRoute")
    flipCard(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let card = player.luck.card
        if (card.cardState[index]) {
            return { err: 100 }//卡片已经翻开
        }
        let flipMax = luckHandle.getFlipTimesMax(player, playerHandle.getVip(player).vipLvl, isMonthVip)
        if (card.flipTimes >= flipMax) {
            let res = gameUtils.useNeedResource(player, gameConfigHandle.gameConfig.need.flipCard, 1)
            if (res !== 0) {
                return { err: 1 }
            }
        }
        card.cardState[index] = true
        card.flipTimes++

        //翻3张获得1张明牌
        let addVisibleNeedFlipTimes = gameConfigHandle.gameConfig.luck.addVisibleNeedFlipTimes
        if (card.flipTimes % addVisibleNeedFlipTimes == 0) {
            let unknownCards = []
            let cardIndexs = gameConfigHandle.gameConfig.luck.cardIndexs.concat()
            cardIndexs.forEach(i => {
                if (!card.visibleCards.includes(i) && !card.cardState[i]) {
                    unknownCards.push(i)
                }
            })
            card.visibleCards.push(utils.getOneFromArray(unknownCards, random))
        }
        let rewardList = []
        // let rewardInfo = luckHandle.getCardRewardType(player)
        let reward = gameConfigHandle.gameConfig.luck.rewardsOfCard[card.cards[index]]
        reward.forEach(it => {
            if (it.id == 'gold') {
                let gold = adventureHandle.getGoldResoureOriginAdd(player) * it.amount
                player.itemInfo.gold += gold
                rewardList.push({ id: 'gold', amount: gold })
            } else {
                bagHandle.addItem(player.bag, Number(it.id), it.amount)
                rewardList.push(it)
            }
        })
        return {
            err: 0, data: rewardList
        }
    }
    @localApi("normalRoute")
    motivateLvlUp(gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let mapConfig = gameConfigHandle.gameConfig.map
        let lvlMax = adventureHandle.getWbossMapMotivateTimes(player, isMonthVip)
        let lvl = player.adventure.bossMapData.motivateLvl
        if (lvl >= lvlMax) {
            return { err: 1 }
        }
        let res = gameUtils.useNeedResource(player, gameConfigHandle.gameConfig.need.motivateLvlUp, lvl)
        if (res !== 0) {
            return { err: 2 }
        }
        player.adventure.bossMapData.motivateLvl++
        return { err: 0 }
    }
    @localApi("normalRoute")
    getBossHurtGift(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let state = player.adventure.bossMapData.getGiftData[index] || false
        if (state) {
            return { err: 100 }
        }
        let mapConfig = gameConfigHandle.gameConfig.map
        let hurtNeed = mapConfig.giftHurtsOfBoss[index]
        let hurtNow = player.adventure.bossMapData.reduceBossHpMax
        if (hurtNow < hurtNeed) {
            return { err: 1 }
        }
        let gifts = mapConfig.giftOfWBossHurt[index].gift
        for (let j = 0; j < gifts.length; j++) {
            let gift = gifts[j]
            // if (gift.id == 'diamond') {
            //     player.itemInfo.diamond += gift.amount
            // } else if (gift.id == 'gold') {
            //     player.itemInfo.gold += gift.amount
            // } else if (gift.id == 'pet') {
            //     if (!bagHandle.isHasEmpty(player.petBag, gift.amount)) {
            //         return { err: 2 }
            //     }
            //     for (let i = 0; i < gift.amount; i++) {
            //         let petID: number = utils.getOneFromArray(gameConfigHandle.gameConfig.pet.callPetList, random)
            //         let item = petHandle.create(petID, 1, random, {})
            //         bagHandle.addItemComponent(player.petBag, item)
            //     }
            // } else {
            bagHandle.addItem(player.bag, Number(gift.id), gift.amount)
            // }
        }
        player.adventure.bossMapData.getGiftData[index] = true
        return { err: 0, data: gifts }
    }

    @localApi("normalRoute")
    saveTime(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.baseInfo.saveTime = time
        return { err: 0 }
    }

    @localApi("normalRoute")
    setItemLock(bagType: BagType, bagIndex: number, lock: boolean, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        if (bagType == BagType.Equipment) {
            let item = player.equipmentBag.items[bagIndex]
            if (item) {
                item.lock = lock
                return { err: 0 }
            }
        } else if (bagType == BagType.Skill) {
            let item = player.skillBag.items[bagIndex]
            if (item) {
                item.lock = lock
                return { err: 0 }
            }
        } else if (bagType == BagType.Pet) {
            let item = player.petBag.items[bagIndex]
            if (item) {
                item.lock = lock
                return { err: 0 }
            }
        }

        return { err: 100 }
    }

    @localApi("normalRoute")
    equip(occType: number, bagIndex: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let item = player.equipmentBag.items[bagIndex]
        if (!item) {
            return { err: 100 }
        }
        let equipments = player.teamEquipment.equipmentList[occType][0]
        if (!equipments) {
            return {
                err: 2
            }
        }
        if (item.equipment.lvl > player.altar.lvl) {
            return { err: 1 }
        }
        let itemConfig = gameConfigHandle.configByID[item.tid]
        let equipmentType = itemConfig.equipmentType
        let originItem = equipments[equipmentType]
        equipments[equipmentType] = item
        bagHandle.removeItemByPosition(player.equipmentBag, bagIndex)
        if (originItem) {
            bagHandle.addItemComponent(player.equipmentBag, originItem)
        }

        equipmentHandle.calcEquipmentAddGoldPer(player)
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    oneKeyEquip(occType: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData


        let equipments = player.teamEquipment.equipmentList[occType][0]
        if (!equipments) {
            return {
                err: 2
            }
        }

        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let wearableList = equipmentConfig.occWearableList[occType]
        let eitems = {}
        equipmentConfig.equipmentListType.forEach(equipmentType => {
            eitems[equipmentType] = []
        })

        player.equipmentBag.items.forEach(item => {
            if (item) {
                let itemConfig = gameConfigHandle.configByID[item.tid]
                if (wearableList[itemConfig.equipmentType].indexOf(itemConfig.equipmentSubType) !== -1) {
                    if (item.equipment.lvl <= player.altar.lvl) {
                        eitems[itemConfig.equipmentType].push(item)
                    }
                }
            }
        })

        for (let i in eitems) {
            eitems[i].sort((a1, a2) => {
                return a2.equipment.power - a1.equipment.power
            })
        }
        for (let part in equipments) {
            let oldequipment = equipments[part]
            if (oldequipment) {
                let itemConfig = gameConfigHandle.configByID[oldequipment.tid]
                if (wearableList[itemConfig.equipmentType] && wearableList[itemConfig.equipmentType].indexOf(itemConfig.equipmentSubType) === -1) {
                    let err = bagHandle.addItemComponent(player.equipmentBag, oldequipment)
                    if (err == false) {
                        return {
                            err: 0,
                            code: 1
                        }
                    }
                    equipments[part] = null
                    oldequipment = null
                }
            }
            if (eitems[part] && eitems[part].length > 0) {
                if (oldequipment && oldequipment.equipment.power > eitems[part][0].equipment.power) {
                } else {
                    equipments[part] = eitems[part][0]
                    bagHandle.removeItemByPosition(player.equipmentBag, eitems[part][0].pos)
                    if (oldequipment) {
                        bagHandle.addItemComponent(player.equipmentBag, oldequipment)
                    }
                }
            }
        }

        equipmentHandle.calcEquipmentAddGoldPer(player)
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    unequip(occType: OccType, equipmentType: any, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let equipments = player.teamEquipment.equipmentList[occType][0]
        if (!equipments) {
            return {
                err: 2
            }
        }
        let originItem = equipments[equipmentType]
        if (!bagHandle.isHasEmpty(player.equipmentBag)) {
            return {
                err: 2
            }
        }
        equipments[equipmentType] = null
        bagHandle.addItemComponent(player.equipmentBag, originItem)
        equipmentHandle.calcEquipmentAddGoldPer(player)
        return {
            err: 0
        }
    }

    /**
     * 设置物品为新品
     * @param bagIndex 
     * @param gameRouteData 
     */
    @localApi("normalRoute")
    setItemNew(bagType: BagType, bagIndex: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        if (bagType == BagType.Equipment) {
            let item = player.equipmentBag.items[bagIndex]
            if (item) {
                item.new = false
                return { err: 0 }
            }
        } else if (bagType == BagType.Pet) {
            let item = player.petBag.items[bagIndex]
            if (item) {
                item.new = false
                return { err: 0 }
            }
        }

        return { err: 100 }
    }

    /**
     * 增加背包容量
     * @param type 
     * @param gameRouteData 
     */
    @localApi("normalRoute")
    addBagSize(type: BagType, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.bagLvlup
        let bag: BagModel
        if (type == BagType.Bag) {
            bag = player.bag
        } else if (type == BagType.Equipment) {
            bag = player.equipmentBag
        } else if (type == BagType.Pet) {
            bag = player.petBag
        }
        if (bag.addSize >= 100) {
            return { err: 2 }
        }
        let res = gameUtils.useNeedResource(player, needConfig, bag.addSize + 1)
        if (res !== 0) {
            return { err: 1 }
        }
        bag.addSize += 1
        bagHandle.initPackag(bag)
        return { err: 0 }
    }
    //背包整理
    @localApi("normalRoute")
    bagArrange(bagType: BagType, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        bagHandle.arrange(player, bagType)
        return {
            err: 0
        }
    }
    //签到
    @localApi("normalRoute")
    daySignIn(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        if (player.baseInfo.daySign == true) {
            return { err: 100 }
        }
        player.baseInfo.daySign = true
        let vipConfig = gameConfigHandle.gameConfig.vip
        let diamond = vipConfig.vipAdd.daySignGetDiamond[vipLvl]
        let getExp = vipConfig.vipAdd.daySignGetVipExp[vipLvl]
        player.itemInfo.diamond += diamond
        player.itemInfo.vipExp += getExp
        return { err: 0, data: { vipExp: getExp, diamond: diamond } }
    }
    @localApi("normalRoute")
    newYearSignIn(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        if (player.baseInfo.newYearSign == true) {
            return { err: 100 }
        }
        player.baseInfo.newYearSign = true
        let vipConfig = gameConfigHandle.gameConfig.vip
        let data = []
        gameConfigHandle.gameConfig.vip.dayShareGet.forEach(item => {
            let itemID = item[0]
            let amount: any = item[1]
            if (isNaN(amount)) {
                return
            }
            if (typeof itemID == 'string') {
                player.itemInfo[itemID] += amount
                data.push([itemID, amount])
            } else if (typeof itemID == 'number') {
                bagHandle.addItem(player.bag, itemID, amount)
                data.push([itemID, amount])
            }
        })
        return { err: 0, data: data }
    }
    //签到
    @localApi("normalRoute")
    share(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        if (player.baseInfo.shareSign1 == true) {
            return { err: 100 }
        }
        player.baseInfo.shareSign1 = true
        // 签到奖励
        let data = []
        gameConfigHandle.gameConfig.vip.dayShareGet.forEach(item => {
            let itemID = item[0]
            let amount: any = item[1]
            if (isNaN(amount)) {
                return
            }
            if (typeof itemID == 'string') {
                player.itemInfo[itemID] += amount
                data.push([itemID, amount])
            } else if (typeof itemID == 'number') {
                bagHandle.addItem(player.bag, itemID, amount)
                data.push([itemID, amount])
            }
        })
        return { err: 0, data }
    }
    //月卡会员签到
    @localApi("normalRoute")
    monthVipSignIn(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        if (player.baseInfo.monthVipSign == true) {
            return { err: 100 }
        }
        if (!bagHandle.isHasEmpty(player.bag)) {
            return { err: 2 }
        }
        player.baseInfo.monthVipSign = true
        let vipConfig = gameConfigHandle.gameConfig.vip
        let diamond = vipConfig.monthVip.daySignGetDiamond
        let getExp = vipConfig.monthVip.daySignGetVipExp
        player.itemInfo.diamond += diamond
        player.itemInfo.vipExp += getExp
        let itemID = vipConfig.monthVip.getItem.itemID
        let itemNum = vipConfig.monthVip.getItem.amount
        bagHandle.addItem(player.bag, itemID, itemNum)
        return { err: 0, data: { vipExp: getExp, diamond: diamond, itemID: itemID, itemNum: itemNum } }
    }
    //至尊卡会员签到
    @localApi("normalRoute")
    yearVipSignIn(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        if (player.baseInfo.yearVipSign == true) {
            return { err: 100 }
        }
        if (!bagHandle.isHasEmpty(player.bag)) {
            return { err: 2 }
        }
        player.baseInfo.yearVipSign = true
        let items = gameConfigHandle.gameConfig.vip.yearVip.getItems
        items.forEach(item => {
            if (typeof item.itemID == 'number') {
                bagHandle.addItem(player.bag, item.itemID, item.amount)
            } else {
                player.itemInfo[item.itemID] += item.amount
            }
        })
        return { err: 0, data: items }
    }
    //每日联动
    @localApi("normalRoute")
    dayGameSignIn(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        if (player.baseInfo.daySign == true) {
            return { err: 100 }
        }
        player.baseInfo.gameDaySign = true
        let vipConfig = gameConfigHandle.gameConfig.vip
        let diamond = vipConfig.dayGameSignInGetDiamond
        player.itemInfo.diamond += diamond
        return { err: 0 }
    }
    /**
     * 领取邮件
     * @param data 
     * @param gameRouteData 
     */
    @localApi("normalRoute")
    getMail(data: any, h: string, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (player.baseInfo.mailList.indexOf(data._id) !== -1) {
            return { err: 50 }
        }
        let items1 = data.items1
        let items2 = data.items2

        if (items1) {
            items1.forEach(item => {
                if (isNaN(item[0]) == false) {
                    bagHandle.addItem(player.bag, Number(item[0]), item[1])
                } else {
                    player.itemInfo[item[0]] += item[1]
                }
                if (!player.ch.mD[item[0]]) {
                    player.ch.mD[item[0]] = 0
                }
                player.ch.mD[item[0]] += item[1]
            })
        }
        if (items2) {
            items2.forEach(e => {
                let item = new ItemModel(e)
                if (item.equipment) {
                    bagHandle.addItemComponent(player.equipmentBag, item)
                } else if (item.skill) {
                    bagHandle.addItemComponent(player.skillBag, item)
                } else if (item.pet) {
                    bagHandle.addItemComponent(player.petBag, item)
                } else {
                    bagHandle.addItemComponent(player.bag, item)
                }
            })
        }
        if (player.baseInfo.mailList.length >= 5) {
            player.baseInfo.mailList.shift()
        }
        player.baseInfo.mailList.push(data._id)
        return { err: 0 }
    }

    @localApi("normalRoute")
    modifyName(gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.modifyName, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        return { err: 0 }
    }
    /**
     * 一键卸下
     * @param partnerIndexID 
     * @param gameRouteData 
     */
    @localApi("normalRoute")
    oneKeyUnEquip(partnerIndexID: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        if (!partner) {
            return {
                err: 1
            }
        }
        let equipments = partner.equipments[0]
        if (!equipments) {
            return {
                err: 2
            }
        }

        for (let part in equipments) {
            let oldequipment = equipments[part]
            if (oldequipment) {
                let err = bagHandle.addItemComponent(player.equipmentBag, oldequipment)
                if (err == false) {
                    return {
                        err: 0,
                        code: 1
                    }
                }
                equipments[part] = null
            }
        }

        return {
            err: 0
        }
    }
    //设置掉落自动分解
    @localApi("normalRoute")
    setAutoDestroy(bagType: BagType, quality: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (bagType == BagType.Equipment) {
            player.adventure.equipmentAutoDestroy[quality] = !player.adventure.equipmentAutoDestroy[quality]
        } else if (bagType == BagType.Skill) {
            player.adventure.skillBookAutoDestroy[quality] = !player.adventure.skillBookAutoDestroy[quality]
        }
        return { err: 0 }
    }
    //一键分解
    @localApi("normalRoute")
    oneKeyDecompose(bagType: BagType, quality: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let bag: BagModel
        let amount = 0, totalSoul = 0
        if (bagType == BagType.Equipment) {
            bag = player.equipmentBag
            let getItems = {}
            bag.items.forEach(item => {
                if (item && item.equipment.quality == quality && item.lock == false
                    && (!item.equipment.packageAttributesId || item.equipment.packageAttributesId == 0)) {
                    amount += 1
                    let getItem = equipmentHandle.destroy(player, item)
                    bagHandle.removeItemByPosition(bag, item.pos)
                    utils.entries(getItem).forEach(item => {
                        let itemID: any = item[0]
                        let amount: any = item[1]
                        if (!getItems[itemID]) {
                            getItems[itemID] = 0
                        }
                        getItems[itemID] += amount
                    })
                }
            })
            return { err: 0, data: { amount, getItems } }
        } else if (bagType == BagType.Skill) {
            bag = player.skillBag
            let getItems = {}
            bag.items.forEach(item => {
                if (item && item.skill.quality == quality && item.lock == false) {
                    amount += 1
                    let getItem = skillHandle.destroy(player, item)
                    bagHandle.removeItemByPosition(bag, item.pos)
                    utils.entries(getItem).forEach(item => {
                        let itemID: any = item[0]
                        let amount: any = item[1]
                        if (!getItems[itemID]) {
                            getItems[itemID] = 0
                        }
                        getItems[itemID] += amount
                    })
                }
            })
            return { err: 0, data: { amount, getItems } }
        } else if (bagType == BagType.Pet) {
            bag = player.petBag
            let getItems = {}
            bag.items.forEach(item => {
                if (item && item.pet.quality == quality && item.pet.lvl <= 1 && item.pet.elements.length < 3 && item.lock == false) {
                    amount += 1
                    let getItem = petHandle.destroy(player, item)
                    utils.entries(getItem).forEach(item => {
                        if (!getItems[item[0]]) {
                            getItems[item[0]] = 0
                        }
                        getItems[item[0]] += item[1]
                    })
                    bagHandle.removeItemByPosition(bag, item.pos)
                }
            })
            return { err: 0, data: { amount, getItems } }
        }

        return { err: 0, data: amount }
    }

    @localApi("normalRoute")
    itemCompose(bagIndex: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let item = player.bag.items[bagIndex]
        if (!item) {
            return { err: 100 }
        }
        let itemConfig = gameConfigHandle.configByID[item.tid]
        if (!itemConfig.lvlUpID && itemConfig.bagTabType != 2) {
            return { err: 100 }
        }
        if (!bagHandle.isHasEmpty(player.bag)) {
            return { err: 2 }
        }
        let lvlupNeed = itemConfig.lvlupNeed
        let randomGet = false
        if (itemConfig.bagTabType == 2) {
            lvlupNeed = {
                items: [
                    { name: item.tid, incValues: [gameConfigHandle.gameConfig.partner.fragmentComposeCost, 0, 0] },
                ]
            }
            randomGet = true
        } else if (itemConfig.itemSubType == 4) {
            let gameConfig = gameConfigHandle.gameConfig
            lvlupNeed = gameConfig.need.speedUpComposeNeed[item.tid]
        }
        let res = gameUtils.useNeedResource(player, lvlupNeed, 1, amount)
        if (res !== 0) {
            return { err: 1 }
        }
        let itemID, itemIDs
        if (randomGet) {
            itemIDs = {}
            if (itemConfig.bagTabType == 2) {
                let itemSubType = itemConfig.itemSubType
                let nextlvl = itemConfig.nextLvl
                if (!nextlvl) {
                    return { err: 3 }
                }
                let randomIDs = Object.entries(gameConfigHandle.gameConfig.items).filter(entry => {
                    let config: any = entry[1]
                    return config.lvl == nextlvl && config.bagTabType == 2 && config.itemSubType == itemSubType
                }).map(entry => {
                    return Number(entry[0])
                })
                for (let i = 0; i < amount; i++) {
                    let tid = utils.getOneFromArray(randomIDs, random)
                    if (itemIDs[tid]) {
                        itemIDs[tid] += 1
                    } else {
                        itemIDs[tid] = 1
                    }
                    let _item = itemHandle.create({ tid, stack: 1 })
                    bagHandle.addItemComponent(player.bag, _item)
                }
            }
        } else {
            itemID = itemConfig.lvlUpID
            let _item = itemHandle.create({ tid: itemID, stack: amount })
            bagHandle.addItemComponent(player.bag, _item)
            if (itemConfig.itemSubType != 4) {
                player.task.gemUpTimes += 1
            }
        }
        return {
            err: 0,
            data: { itemID, itemIDs, amount }
        }
    }

    @localApi("normalRoute")
    itemOpen(bagIndex: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random, isMonthVip, isYearVip } = gameRouteData
        let item = player.bag.items[bagIndex]
        let itemConfig = gameConfigHandle.configByID[item.tid]
        let getItem: any = {}
        if (amount > item.stack) {
            return { err: 51 }
        }
        if (itemConfig.items) {
            for (let i = 0; i < amount; i++) {
                itemConfig.items.forEach(itemConfig => {
                    if (random.intn(100) < itemConfig.rate) {
                        let itemList = itemConfig.itemList
                        let itemID: any = utils.getOneFromArray(itemList, random)
                        if (!getItem[itemID]) {
                            getItem[itemID] = 0
                        }
                        if (itemConfig.linkResource) {
                            getItem[itemID] += (itemConfig.value || 1) * adventureHandle.getGoldResoureAdd(player, isMonthVip, isYearVip)
                        } else {
                            getItem[itemID] += itemConfig.value || 1
                        }
                    }
                })
            }
            bagHandle.removeSomeItemByPosition(player.bag, bagIndex, amount)
            if (!player.useInfo[item.tid]) {
                player.useInfo[item.tid] = 0
            }
            player.useInfo[item.tid] += amount
            utils.entries(getItem).forEach(item => {
                let itemID: any = item[0]
                let amount: any = item[1]
                if (isNaN(itemID)) {
                    player.itemInfo[itemID] += amount
                } else {
                    let item = itemHandle.create({ tid: Number(itemID), stack: amount })
                    bagHandle.addItemComponent(player.bag, item)

                }
            })
            return { err: 0, data: getItem }
        } else {
            return { err: 100 }
        }

    }

    @localApi("normalRoute")
    itemOpenSelect(bagIndex: number, index: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let item = player.bag.items[bagIndex]
        let itemConfig = gameConfigHandle.configByID[item.tid]
        if (amount > item.stack) {
            return { err: 51 }
        }
        if (!bagHandle.isHasEmpty(player.bag)) {
            return { err: 2 }
        }
        if (!itemConfig.selectItems || !itemConfig.selectItems[index]) {
            return { err: 52 }
        }

        let itemID = itemConfig.selectItems[index]
        bagHandle.removeSomeItemByPosition(player.bag, bagIndex, amount)
        if (!player.useInfo[item.tid]) {
            player.useInfo[item.tid] = 0
        }
        player.useInfo[item.tid] += amount
        let getItem = itemHandle.create({ tid: Number(itemID), stack: amount })
        bagHandle.addItemComponent(player.bag, getItem)
        return { err: 0, data: { itemID, amount } }
    }

    @localApi("normalRoute")
    itemExchange(bagIndex: number, selectItemID: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let item = player.bag.items[bagIndex]
        let itemConfig = gameConfigHandle.configByID[item.tid]
        let exchangeCost = gameConfigHandle.gameConfig.partner.fragmentExchangeCost
        let cost = amount * exchangeCost || 0
        if (cost > item.stack || cost == 0) {
            return { err: 1 }
        }
        let existNum = bagHandle.getTotalNumByItemId(player.bag, selectItemID)
        if (!existNum && !bagHandle.isHasEmpty(player.bag)) {
            return { err: 2 }
        }
        let eItemConfig = gameConfigHandle.configByID[selectItemID]
        if (!eItemConfig || eItemConfig.itemSubType != itemConfig.itemSubType || eItemConfig.lvl != itemConfig.lvl) {
            return { err: 3 }
        }
        bagHandle.removeSomeItemByPosition(player.bag, bagIndex, cost)
        let getItem = itemHandle.create({ tid: selectItemID, stack: amount })
        bagHandle.addItemComponent(player.bag, getItem)
        return { err: 0, data: { itemID: selectItemID, amount } }
    }

    @localApi("normalRoute")
    callSkillBook(amount: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let code = 0, times = 0
        let skillConfig = gameConfigHandle.gameConfig.skill
        for (let i = 0; i < amount; i++) {
            if (!bagHandle.isHasEmpty(player.skillBag)) {
                code = 2
                break
            }

            let item = skillHandle.createBook(random, {})
            let res = bagHandle.addItemComponent(player.skillBag, item)
            times += 1
        }

        return {
            err: 0, data: { code, times }
        }
    }

    @localApi("normalRoute")
    replaceSkillBook(index, bagIndex: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData

        let item = player.skillBag.items[bagIndex]
        if (!item) {
            return { err: 100 }
        }
        let skillID = item.skill.tid
        let partner = playerHandle.getPartner(player)
        for (let i = 0; i < partner.skillList.length; i++) {
            let skillItem = partner.skillList[i]
            if (skillItem && skillItem.skill.tid == skillID) {
                return { err: 2 }
            }
        }
        let oldItem = partner.skillList[index]
        partner.skillList[index] = item
        bagHandle.removeItemByPosition(player.skillBag, bagIndex)
        if (oldItem) {
            bagHandle.addItemComponent(player.skillBag, oldItem)
        }

        return {
            err: 0,
        }
    }


    @localApi("normalRoute")
    resetBaseAttri(partnerIndexID: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.resetBaseAttri
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let partnerConfig = gameConfigHandle.gameConfig.partner
        utils.keys(partnerConfig.baseAttri).forEach(key => {
            partner.baseAttris[key] = 0
        })
        let baseAttris = utils.keys(partnerConfig.baseAttri).slice(0)
        for (let i = 1; i < partner.lvl; i++) {
            let type = utils.getOneFromArray(baseAttris, random)
            partner.baseAttris[type] += 1
        }


        return { err: 0 }
    }

    @localApi("normalRoute")
    baseAttriLvlup(partnerIndexID: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let lvl = partner.baseAttris.lvl

        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.partnerBaseAttriLvlupNeed, lvl + 1)
        if (res !== 0) {
            return { err: 1 }
        }
        partner.baseAttris.lvl += 1

        return { err: 0 }
    }


    @localApi("normalRoute")
    addBaseAttri(partnerIndexID: number, type: number, value: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        let { totalPoint, usePoint, remainPoint } = partnerHandle.getBaseAttirPoint(player)
        if (value > remainPoint) {
            return { err: 100 }
        }
        partner.baseAttris[type] += value

        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    refreshMarket(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        let gameConfig = gameConfigHandle.gameConfig
        let marketConfig = gameConfig.market
        let maxFreeRefreshTimes = marketConfig.maxFreeRefreshTimes
        if (player.market.freeTimes >= maxFreeRefreshTimes) {
            let maxDiamondRefreshTimes = gameConfig.market.maxDiamondRefreshTimes // 普通钻石次数
                + (gameConfig.vip.vipAdd.marketFreshTimes[vipLvl] || 0)  // vip增加次数
                + (isMonthVip ? gameConfig.vip.monthVip.marketFreshTimes : 0) // 月卡增加次数
            if (player.market.refreshTimes >= maxDiamondRefreshTimes) {
                return { err: 2 }
            }
            let needConfig = gameConfigHandle.gameConfig.need.marketRefreshNeed
            let refreshTimes = player.market.refreshTimes
            if (refreshTimes > marketConfig.maxRefreshTimes) {
                refreshTimes = marketConfig.maxRefreshTimes
            }
            let res = gameUtils.useNeedResource(player, needConfig, refreshTimes)
            if (res !== 0) {
                return { err: 1 }
            }
            player.market.refreshTimes += 1
        } else {
            player.market.freeTimes += 1
        }
        player.market.totalAmount += 1
        marketHandle.create(player, random)
        return { err: 0 }
    }
    @localApi("normalRoute")
    noWorshipNotice(gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        player.market.worshipNotice = true
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    marketBuy(index: number, gameRouteData?: GameRouteData) {
        let { player, random, vipLvl, isMonthVip } = gameRouteData
        let item = player.market.items[index]
        if (!item) {
            return { err: 100 }
        }
        let needDiamond = 0, needGold = 0
        if (item.needDiamond !== 0) {
            needDiamond = Math.floor(item.needDiamond * item.discount / 100)
            if (isMonthVip && item.monthDiscount !== 100) {
                needDiamond = Math.floor(item.needDiamond * item.monthDiscount / 100)
            }

            if (player.itemInfo.diamond < needDiamond) {
                return { err: 1 }
            }
        } else if (item.needGold !== 0) {
            needGold = item.needGold
            if (player.itemInfo.gold < needGold) {
                return { err: 3 }
            }
        }

        if (item.itemId !== 0) {
            if (typeof item.itemId == "string") {
                player.itemInfo[item.itemId] += item.amount
            } else {
                let res = bagHandle.addItem(player.bag, item.itemId, item.amount)
                if (!res) {
                    return { err: 2 }
                }
            }
        }
        if (item.item) {
            if (item.item.skill) {
                if (!bagHandle.isHasEmpty(player.skillBag)) {
                    return { err: 2 }
                }
                bagHandle.addItemComponent(player.skillBag, item.item)
            } else if (item.item.equipment) {
                if (!bagHandle.isHasEmpty(player.equipmentBag)) {
                    return { err: 2 }
                }
                bagHandle.addItemComponent(player.equipmentBag, item.item)
            } else if (item.item.pet) {
                if (!bagHandle.isHasEmpty(player.petBag)) {
                    return { err: 2 }
                }
                bagHandle.addItemComponent(player.petBag, item.item)
            }
        }
        player.itemInfo.diamond -= needDiamond
        player.itemInfo.gold -= needGold
        player.market.buyTotalAmount += 1
        player.market.buyDayAmount += 1
        player.market.items.splice(index, 1)
        marketHandle.addExp(player, 1)
        return { err: 0, data: item }
    }

    @localApi("normalRoute")
    mapChallenge(mapID: number, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let mapProgerss = player.adventure.mapProgress
        let mapConfig = gameConfigHandle.gameConfig.map
        let mapItemConfig = gameConfigHandle.configByID[mapID]
        let progress = mapProgerss[mapID] || 0
        let maxTimes = adventureHandle.getInstanceAmount(player, isMonthVip)

        if (player.adventure.instanceAmount >= maxTimes) {
            return {
                err: 1
            }
        }
        if (progress >= mapItemConfig.maxFloor) {
            return {
                err: 3
            }
        }

        let mapGet = {}

        if (mapItemConfig.get) {
            mapItemConfig.get.forEach(item => {
                let itemID = item.itemID
                if (itemID) {
                    let values = item.incValues
                    let value = utils.getIncValues(values, progress)
                    value = Math.floor(value)
                    if (!mapGet[itemID]) {
                        mapGet[itemID] = 0
                    }
                    mapGet[itemID] += value
                    bagHandle.addItem(player.bag, itemID, value)
                }
                if (item.itemIDs) {
                    let itemID: number = utils.getOneFromArray(item.ItemIDs, random)
                    let values = item.Values
                    let value = utils.getIncValues(values, progress)
                    value = Math.floor(value)
                    if (!mapGet[itemID]) {
                        mapGet[itemID] = 0
                    }
                    mapGet[itemID] += value
                    bagHandle.addItem(player.bag, itemID, value)
                }
            })
        }
        if (mapItemConfig.getResource) {
            mapItemConfig.getResource.forEach(item => {
                let itemID = item.itemID
                let values = item.incValues
                let mapLvl = utils.getIncValues(mapItemConfig.MapLvl, progress)
                let value = utils.getIncValues(values, progress) * gameUtils.getResourceAmount(itemID, mapLvl)
                value = Math.floor(value)
                if (!mapGet[itemID]) {
                    mapGet[itemID] = 0
                }
                mapGet[itemID] += value
                if (typeof itemID == 'number') {
                    bagHandle.addItem(player.bag, itemID, value)
                } else {
                    player.itemInfo[itemID] += value
                }
            })
        }
        player.adventure.instanceAmount += 1
        return {
            err: 0,
            data: {
                mapID,
                floor: progress,
                mapGet,
            }
        }
    }

    @localApi("normalRoute")
    equipmentStr(tid: number, type: EquipmentType, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let code = 0
        let times = 0
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let partner = playerHandle.getPartnerByIndexID(player, tid)
        let equipmentsStrList: EquipmentsLvlupModel = partner.equipmentsStr
        for (let i = 0; i < amount; i++) {
            if (equipmentsStrList[type] >= partner.lvl) {
                code = 2
                break
            }

            let needConfig = gameConfigHandle.gameConfig.need
            let err = gameUtils.useNeedResource(player, needConfig.equipmentStrNeed, equipmentsStrList[type])
            if (err !== 0) {
                code = 1
                break
            }
            equipmentsStrList[type] += 1
            times += 1
        }

        return { err: 0, data: { code: code, times } }
    }

    @localApi("normalRoute")
    equipmentOneKeyStr(tid: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let code = 0
        let times = 0
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let partner = playerHandle.getPartnerByIndexID(player, tid)
        let equipmentsStrList: EquipmentsLvlupModel = partner.equipmentsStr
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let overFlag = false
        for (let i = 0; i < amount; i++) {
            for (let j = 0; j < equipmentConfig.equipmentListType.length; j++) {
                let type = equipmentConfig.equipmentListType[j]
                if (equipmentsStrList[type] >= partner.lvl) {
                    code = 2
                    overFlag = true
                    break
                }
                let needConfig = gameConfigHandle.gameConfig.need
                let err = gameUtils.useNeedResource(player, needConfig.equipmentStrNeed, equipmentsStrList[type])
                if (err !== 0) {
                    code = 1
                    overFlag = true
                    break
                }
                equipmentsStrList[type] += 1
                times += 1
            }
            if (overFlag) {
                break
            }
        }
        return { err: 0, data: { code: code, times } }
    }

    @localApi("normalRoute")
    partnerLvlup(partnerIndex: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndex)
        let needConfig = gameConfigHandle.gameConfig.need
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let times = 0
        let code = 0
        if (playerHandle.isMustAdjustTeam(player)) {
            return { err: 3 }
        }
        for (let i = 0; i < amount; i++) {
            if (partner.lvl >= player.altar.lvl) {
                code = 3
                break
            }
            let res = gameUtils.useNeedResource(player, needConfig.partnerLvlupNeed, partner.lvl)
            if (res !== 0) {
                code = 1
                break
            }
            partner.lvl += 1
            times += 1
        }
        player.team.teamList.forEach(tid => {
            if (tid) {
                player.team.lockList[tid] = true
            }
        })
        return { err: 0, data: { times, code } }
    }

    @localApi("normalRoute")
    altarLvlup(amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData

        let needConfig = gameConfigHandle.gameConfig.need
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let times = 0
        let code = 0
        for (let i = 0; i < amount; i++) {
            if (player.altar.lvl >= partnerConfig.maxLvl) {
                code = 3
                break
            }
            let res = gameUtils.useNeedResource(player, needConfig.partnerLvlupNeed, player.altar.lvl)
            if (res !== 0) {
                code = 1
                break
            }
            let baseAttris = utils.keys(partnerConfig.baseAttri).slice(0)
            let type = utils.getOneFromArray(baseAttris, random)
            player.altar.lvl += 1
            times += 1
        }
        player.team.teamList.forEach(tid => {
            if (tid) {
                player.team.lockList[tid] = true
            }
        })
        return { err: 0, data: { times, code } }
    }

    @localApi("normalRoute")
    equipmentPutInGem(partnerIndexID: number, equipmentType: EquipmentType, gemIndex: number, itemBagIndex: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        let gemItem = player.bag.items[itemBagIndex]
        if (!gemItem) {
            return { err: 100 }
        }
        let gemItemConfig = gameConfigHandle.configByID[gemItem.tid]
        if (!gemItemConfig.gemType) {
            return { err: 101 }
        }
        let originGemItem = partner.equipmentsGem[equipmentType][gemIndex]
        if (originGemItem) {
            let res = bagHandle.addItemComponent(player.bag, originGemItem)
            if (!res) {
                return { err: 1 }
            }
        }
        let gem = itemHandle.create({ tid: gemItem.tid, stack: 1 })
        partner.equipmentsGem[equipmentType][gemIndex] = gem
        bagHandle.removeSomeItemByPosition(player.bag, itemBagIndex, 1)
        return { err: 0 }
    }

    @localApi("normalRoute")
    equipmentRemoveGem(partnerIndexID: number, equipmentType: EquipmentType, gemIndex: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        let originGemItem = partner.equipmentsGem[equipmentType][gemIndex]
        if (originGemItem) {
            let res = bagHandle.addItemComponent(player.bag, originGemItem)
            if (!res) {
                return { err: 1 }
            }
        }
        partner.equipmentsGem[equipmentType][gemIndex] = null
        return { err: 0 }
    }

    @localApi("normalRoute")
    resetTalent(index: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let lockNodeAmount = talentHandle.getTalentLockNodeAmount(player, index)
        let res = gameUtils.useNeedResource(player, needConfig.resetTalent, lockNodeAmount)
        if (res !== 0) {
            return { err: 1 }
        }
        talentHandle.create(player, index, random)
        adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    resetGod(index: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let lockNodeAmount = talentHandle.getGodLockNodeAmount(player, index)
        let res = gameUtils.useNeedResource(player, needConfig.resetGod, lockNodeAmount)
        if (res !== 0) {
            return { err: 1 }
        }
        talentHandle.createGod(player, index, random)
        // adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    resetGodNode(index: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let list = player.god.list[index]
        // list.forEach(item => {
        //     item.lvl = 0
        // })
        // adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    godLvlup(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let talentConfig = gameConfigHandle.gameConfig.talent
        if (player.god.lvl >= talentConfig.godLvlMax) {
            return { err: 2 }
        }
        let res = gameUtils.useNeedResource(player, needConfig.godLvlup, player.god.lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.god.lvl += 1
        // adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    godStrLvlup(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let talentConfig = gameConfigHandle.gameConfig.talent
        if (player.god.strLvl >= talentConfig.godStrLvlMax) {
            return { err: 2 }
        }
        let res = gameUtils.useNeedResource(player, needConfig.godStrLvlup, player.god.strLvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.god.strLvl += 1
        // adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    resetGodStr(godIndex: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let talentConfig = gameConfigHandle.gameConfig.talent
        let res = gameUtils.useNeedResource(player, needConfig.resetGodStr, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let strAmountConfig = talentConfig.godStrAmount
        let strAmount = random.getRandomNumber(strAmountConfig[0], strAmountConfig[1])
        player.god.strList[godIndex] = utils.getRandomArrayNoRepeat(talentConfig.godIndexs, strAmount, random)
        // adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    godNodeLvlup(nodeIndex: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let talentConfig = gameConfigHandle.gameConfig.talent
        let lvl = player.god.nodeLvls[nodeIndex] || 0
        if (lvl >= talentConfig.godNodeLvlMax) {
            return { err: 3 }
        }
        let nodeTypeLvl = talentConfig.godNodeTypeLvl[nodeIndex]
        let needConfig = gameConfigHandle.gameConfig.need.godLvlup[nodeTypeLvl]
        let res = gameUtils.useNeedResource(player, needConfig, lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        let uplvl = 1
        player.god.nodeLvls[nodeIndex] = lvl + 1
        for (let i = 0; i < amount - 1; i++) {
            if (player.god.nodeLvls[nodeIndex] < talentConfig.godNodeLvlMax &&
                gameUtils.getNeedResource(player, needConfig, player.god.nodeLvls[nodeIndex]).err == 0) {
                gameUtils.useNeedResource(player, needConfig, player.god.nodeLvls[nodeIndex])
                player.god.nodeLvls[nodeIndex]++
                uplvl++
            } else {
                break
            }
        }
        // adventureHandle.calcTalentResoureAdd(player)
        return { err: 0, data: uplvl }
    }
    @localApi("normalRoute")
    godNodeLock(index: number, nodeIndex: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let node = player.god.list[index][nodeIndex]
        let lockAmount = talentHandle.getGodLockNodeAmount(player, index)
        if (!node.lock && lockAmount >= gameConfigHandle.gameConfig.talent.lockGodNodeAmountMax) {
            return { err: 1 }
        }
        node.lock = !node.lock
        return { err: 0 }
    }
    @localApi("normalRoute")
    godNodeUnlock(nodeIndex: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let god = player.god
        let unlockAmount = god.unlockNodes.length
        let nodeTypeLvl = gameConfigHandle.gameConfig.talent.godNodeTypeLvl[nodeIndex]
        if (nodeTypeLvl == 0 || god.unlockNodes.includes(nodeIndex)) {
            return { err: 100 }
        }
        let needConfig = gameConfigHandle.gameConfig.need.godNodeUnlock
        let res = gameUtils.useNeedResource(player, needConfig, unlockAmount)
        if (res !== 0) {
            return { err: 1 }
        }
        god.unlockNodes.push(nodeIndex)
        return { err: 0 }
    }

    @localApi("normalRoute")
    useGod(index: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.god.activeIndex = index
        // adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }

    @localApi("normalRoute")
    resetTalentNode(talentIndex: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let list = player.talent.list[talentIndex]
        list.forEach(items => {
            items.forEach(node => {
                node.lvl = 0
            })
        })
        adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }

    @localApi("normalRoute")
    talentLvlup(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let talentConfig = gameConfigHandle.gameConfig.talent
        if (player.talent.lvl >= talentConfig.maxLvl) {
            return { err: 2 }
        }
        let res = gameUtils.useNeedResource(player, needConfig.talentLvlup, player.talent.lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.talent.lvl += 1
        adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }

    @localApi("normalRoute")
    talentNodeLvlup(talentIndex: number, x: number, y: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let talentConfig = gameConfigHandle.gameConfig.talent
        let { usePoint, rowNeedPoints, rowPoints, remainPoint } = talentHandle.getTalentPoint(player, talentIndex)
        let node = player.talent.list[talentIndex][y][x]
        if (node.lvl >= talentConfig.talentNodeMaxLvl[node.type]) {
            return { err: 3 }
        }
        let needPoint = talentConfig.talentNodePoint[node.type]
        if (remainPoint < needPoint) {
            return { err: 1 }
        }
        let nodeTypeLvl = talentConfig.nodeTypeLvl[y * 6 + x]
        if (nodeTypeLvl > 0) {
            let nodeAroundPoint = talentHandle.getNodeAroundPoint(player, talentIndex, y, x)
            if (talentConfig.talentNodeNeedPrePoint[nodeTypeLvl] > nodeAroundPoint) {
                return { err: 2, data: nodeAroundPoint }
            }
        }
        node.lvl += 1
        adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    talentNodeLock(talentIndex: number, x: number, y: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let node = player.talent.list[talentIndex][y][x]
        node.lock = !node.lock
        return { err: 0 }
    }

    @localApi("normalRoute")
    useTalent(talentIndex: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.talent.activeIndex = talentIndex
        adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }

    @localApi("normalRoute")
    addTreasure(id: number, index: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        // if (!player.treasure.list[id]) {
        //     player.treasure.list[id] = new TreasureItemModel()
        // }
        // let treasureItemConfig = gameConfigHandle.configByID[id]
        // let treasureItem: TreasureItemModel = player.treasure.list[id]
        // if (!treasureItem.amounts[index]) {
        //     treasureItem.amounts[index] = 0
        // }
        // let lvl = treasureItem.lvl
        // let maxLvl = treasureItemConfig.maxLvl || 1
        // if (lvl >= maxLvl) {
        //     return { err: 100 }
        // }
        // let needItemid = treasureItemConfig.need[index].itemID
        // let maxAmount = utils.getIncValues(treasureItemConfig.need[index].amount, lvl)
        // if (lvl >= 10) {
        //     maxAmount += Math.ceil(utils.getIncValues(treasureItemConfig.need[index].amount, lvl - 10) * 2)
        // }
        // if (treasureItem.amounts[index] >= maxAmount) {
        //     return { err: 101 }
        // }
        // if (treasureItem.amounts[index] + amount > maxAmount) {
        //     amount = maxAmount - treasureItem.amounts[index]
        // }
        // if (player.treasure.items[needItemid] < amount) {
        //     return { err: 1 }
        // }
        // player.treasure.items[needItemid] -= amount
        // treasureItem.amounts[index] += amount
        // if (treasureItem.amounts[index] == maxAmount) {
        //     let maxFlag = true
        //     treasureItemConfig.need.map((item, i) => {
        //         let itemid = item.itemID
        //         let amounts = item.amount
        //         let needAmount = utils.getIncValues(amounts, lvl)
        //         if (lvl >= 10) {
        //             needAmount += Math.ceil(utils.getIncValues(amounts, lvl - 10) * 2)
        //         }
        //         let amount = treasureItem.amounts[i] || 0
        //         if (amount < needAmount) {
        //             maxFlag = false
        //         }

        //     })
        //     if (maxFlag) {
        //         treasureItem.lvl += 1
        //     }
        // }

        return { err: 0 }
    }
    @localApi("normalRoute")
    treasureStarUp(id: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (!player.treasure.list[id]) {
            player.treasure.list[id] = new TreasureItemModel()
        }
        let treasureConfig = gameConfigHandle.gameConfig.treasure
        let star = player.treasure.list[id].star || 0
        if (star >= treasureConfig.starMax) {
            return { err: 2 }
        }
        let treasureItemConfig = gameConfigHandle.configByID[id]
        let res = gameUtils.useNeedResource(player, treasureItemConfig.starNeed, star)
        if (res !== 0) {
            return { err: 1 }
        }
        player.treasure.list[id].star = star + 1
        adventureHandle.calcTreasureAdventureAdd(player)
        return { err: 0 }
    }
    @localApi("normalRoute")
    treasureLvlup(id: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (!player.treasure.list[id]) {
            player.treasure.list[id] = new TreasureItemModel()
        }
        let treasureConfig = gameConfigHandle.gameConfig.treasure
        if (player.treasure.list[id].lvl >= treasureConfig.maxLvl) {
            return { err: 2 }
        }
        let treasureItemConfig = gameConfigHandle.configByID[id]
        let res = gameUtils.useNeedResource(player, treasureItemConfig.need, player.treasure.list[id].lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.treasure.list[id].lvl += 1
        adventureHandle.calcTreasureAdventureAdd(player)
        return { err: 0 }
    }

    @localApi("normalRoute")
    resetTreasure(gameRouteData?: GameRouteData) {
        // let { player, random } = gameRouteData
        // let needConfig = gameConfigHandle.gameConfig.need
        // let res = gameUtils.useNeedResource(player, needConfig.resetTreasure, 1)
        // if (res !== 0) {
        //     return { err: 1 }
        // }
        // player.treasure.items = [0, 0, 0, 0, 0, 0, 0, 0]
        // for (let i = 0; i < player.treasure.lvl * 2; i++) {
        //     let index = utils.getRandomNumber(0, 5, random)
        //     player.treasure.items[index] += 1
        // }
        // player.treasure.list = {}

        return { err: 0 }
    }

    @localApi("normalRoute")
    setTalentActive(index: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.talent.activeIndex = index
        return { err: 0 }
    }

    @localApi("normalRoute")
    removeSkillBook(partnerIndexID: number, index, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        let oldItem = partner.skillList[index]
        if (oldItem) {
            let err = bagHandle.addItemComponent(player.skillBag, oldItem)
            if (!err) {
                return { err: 1 }
            }
        }
        partner.skillList[index] = null
        return {
            err: 0,
        }
    }


    @localApi("normalRoute")
    setlSkill(partnerIndexID: number, index: number, skillID: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        for (let i = 0; i < partner.lskillList.length; i++) {
            let _skillID = partner.lskillList[i]
            if (_skillID == skillID) {
                partner.lskillList[i] = null
            }
        }
        partner.lskillList[index] = skillID
        return {
            err: 0,
        }
    }

    @localApi("normalRoute")
    skillLvlup(skillID: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let skillLvl = player.skillLvls[skillID] || 1
        if (skillLvl >= gameConfigHandle.gameConfig.skill.skillMaxlvl) {
            return { err: 2 }
        }
        let needConfig = gameConfigHandle.gameConfig.need
        // let res = gameUtils.useNeedResource(player, needConfig.skillLvlup[skillID], skillLvl)
        // if (res !== 0) {
        //     return { err: 1 }
        // }
        if (!player.skillLvls[skillID]) {
            player.skillLvls[skillID] = 1
        }
        player.skillLvls[skillID] += 1

        return {
            err: 0,
        }
    }

    @localApi("normalRoute")
    lskillLvlup(skillID: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let skillLvl = player.lskillLvls[skillID] || 1
        if (skillLvl >= gameConfigHandle.gameConfig.skill.lskillMaxlvl) {
            return { err: 2 }
        }
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.lskillLvlup, skillLvl)
        if (res !== 0) {
            return { err: 1 }
        }
        if (!player.lskillLvls[skillID]) {
            player.lskillLvls[skillID] = 1
        }
        player.lskillLvls[skillID] += 1

        return {
            err: 0,
        }
    }

    @localApi("normalRoute")
    askillLvlup(bagIndex, skillIndex, askillIndex: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let item: ItemModel
        if (!utils.isNull(bagIndex)) {
            item = player.skillBag.items[bagIndex]
        } else if (!utils.isNull(skillIndex)) {
            let partner = playerHandle.getPartner(player)
            item = partner.skillList[skillIndex]

        }
        if (!item) {
            return { err: 100 }
        }

        let aSkillItem = item.skill.aSkillIDs[askillIndex]
        if (!aSkillItem) {
            return { err: 101 }
        }
        let skillConfig = gameConfigHandle.gameConfig.skill
        if (aSkillItem.lvl >= skillConfig.lskillMaxlvl) {
            return { err: 2 }
        }
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.askillLvlup, aSkillItem.lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        aSkillItem.lvl += 1

        return {
            err: 0,
        }
    }

    @localApi("normalRoute")
    adjustAskillLvl(bagIndex, skillIndex, askillIndex: number, value: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let item: ItemModel
        if (!utils.isNull(bagIndex)) {
            item = player.skillBag.items[bagIndex]
        } else if (!utils.isNull(skillIndex)) {
            let partner = playerHandle.getPartner(player)
            item = partner.skillList[skillIndex]

        }
        if (!item) {
            return { err: 100 }
        }

        let aSkillItem = item.skill.aSkillIDs[askillIndex]
        if (!aSkillItem) {
            return { err: 101 }
        }
        if (value !== 1 && value !== -1) {
            return { err: 102 }
        }
        let skillConfig = gameConfigHandle.gameConfig.skill

        if (utils.isNull(aSkillItem.lvl2)) {
            aSkillItem.lvl2 = aSkillItem.lvl
        }
        if (aSkillItem.lvl2 + value > aSkillItem.lvl || aSkillItem.lvl2 + value < 1) {
            return { err: 3 }
        }
        aSkillItem.lvl2 += value
        return {
            err: 0,
        }
    }

    @localApi("normalRoute")
    itemDestroy(bagType: BagType, bagIndex: number, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        if (bagType == BagType.Equipment) {
            if (!bagHandle.isHasEmpty(player.bag)) {
                return { err: 1 }
            }
            let item = player.equipmentBag.items[bagIndex]
            let getItems = equipmentHandle.destroy(player, item)
            bagHandle.removeItemByPosition(player.equipmentBag, bagIndex)
            return {
                err: 0, data: getItems
            }
        } else if (bagType == BagType.Skill) {
            if (!bagHandle.isHasEmpty(player.bag)) {
                return { err: 1 }
            }
            let item = player.skillBag.items[bagIndex]
            let getItems = skillHandle.destroy(player, item)
            bagHandle.removeItemByPosition(player.skillBag, bagIndex)
            return {
                err: 0, data: getItems
            }
        } else if (bagType == BagType.Pet) {
            if (!bagHandle.isHasEmpty(player.bag)) {
                return { err: 1 }
            }
            let item = player.petBag.items[bagIndex]
            let getItems = petHandle.destroy(player, item)
            bagHandle.removeItemByPosition(player.petBag, bagIndex)
            return {
                err: 0, data: getItems
            }
        }
        return {
            err: 2,
        }
    }

    @localApi("normalRoute")
    lockTalent(gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.talentLock[player.talent.lockAmount], 1)
        if (res !== 0) {
            return { err: 1 }
        }
        talentHandle.create(player, player.talent.lockAmount, random)
        player.talent.lockAmount += 1
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    lockGod(gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.godLock[player.god.lockAmount], 1)
        if (res !== 0) {
            return { err: 1 }
        }
        talentHandle.createGod(player, player.god.lockAmount, random)
        player.god.lockAmount += 1
        return {
            err: 0
        }
    }


    @localApi("normalRoute")
    equipPet(battlePos: number, bagIndex: number, gameRouteData?: GameRouteData) {
        let { player, isMonthVip, isYearVip } = gameRouteData
        let item = player.petBag.items[bagIndex]
        if (!item) {
            return { err: 100 }
        }
        let partner = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos])
        let originItem = partner.pet
        partner.pet = item
        item.new = false
        bagHandle.removeItemByPosition(player.petBag, bagIndex)
        if (originItem) {
            originItem.new = false
            bagHandle.addItemComponent(player.petBag, originItem)
        }
        playerHandle.initTeamFighterAttri(player, isMonthVip, isYearVip)
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    lockAltar(type: string, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        if (type == "pet") {
            let petConfig = gameConfigHandle.gameConfig.pet
            let maxAltarAmount = petConfig.altarAmount
            let altarAmount = player.altarPets.altarAddAmount + petConfig.initAltarAmount
            if (altarAmount >= maxAltarAmount) {
                return { err: 100 }
            }
            let res = gameUtils.useNeedResource(player, needConfig.petAltarLockNeed[player.altarPets.altarAddAmount], 1)
            if (res !== 0) {
                return { err: 1 }
            }
            player.altarPets.altarAddAmount += 1
        }
        return { err: 0 }
    }
    @localApi("normalRoute")
    altarLvlUp(gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let lvlMax = gameConfigHandle.gameConfig.pet.altarLvlMax
        let lvl = player.altarPets.altarLvl
        if (lvl >= lvlMax) {
            return { err: 2 }
        }
        // let unlockAltarLvlUpFloor = gameConfigHandle.gameConfig.pet.unlockAltarLvlUpFloor
        // if (player.adventure.floor < unlockAltarLvlUpFloor) {
        //     return { err: 3 }
        // }
        let needConfig = gameConfigHandle.gameConfig.need.petAltarlvlUp
        let res = gameUtils.useNeedResource(player, needConfig, lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.altarPets.altarLvl = lvl + 1
        return { err: 0 }
    }
    @localApi("normalRoute")
    unequipPet(battlePos: number, index: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos])
        // let originItem = pets.petsInAltar[index]
        let originItem = player.altarPets.petsInAltar[index]
        if (utils.isNull(index)) {
            originItem = partner.pet
        }
        // if (!bagHandle.isHasEmpty(player.petBag)) {
        //     return {
        //         err: 2
        //     }
        // }
        if (originItem) {
            if (bagHandle.isHasEmpty(player.petBag, 1)) {
                if (utils.isNull(index)) {
                    partner.pet = null
                } else {
                    player.altarPets.petsInAltar[index] = null
                }

                bagHandle.addItemComponent(player.petBag, originItem)
            } else {
                return {
                    err: 1
                }
            }
        }
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    petWashAttri(bagIndex, altarIndex: number, battlePos: number, lock: boolean[], gameRouteData?: GameRouteData) {
        let { player, random, vipLvl } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos])
        let petItem: ItemModel
        if (!utils.isNull(bagIndex)) {
            petItem = player.petBag.items[bagIndex]
        } else if (!utils.isNull(altarIndex)) {
            petItem = player.altarPets.petsInAltar[altarIndex]
        } else {
            petItem = partner.pet
        }
        if (!petItem) {
            return { err: 100 }
        }
        let total = 0
        lock.forEach(value => {
            if (value) {
                total += 1
            }
        })
        let needConfig = gameConfigHandle.gameConfig.need.petWashAttri
        let res = gameUtils.useNeedResource(player, needConfig, total + 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let totalStar = 0
        let petConfig = gameConfigHandle.gameConfig.pet;
        let petItemConfig = gameConfigHandle.configByID[petItem.tid]
        for (let j = 0; j < petItemConfig.attris.length; j++) {
            if (!lock[j]) {
                totalStar += petItem.pet.attriQualitys2[j] || 0
                petItem.pet.attriQualitys2[j] = 0
            }
        }
        for (let i = 0; i < totalStar; i++) {
            let list = []
            for (let j = 0; j < petItemConfig.attris.length; j++) {
                if (!lock[j] && petItem.pet.attriQualitys2[j] < petConfig.starMax) {
                    list.push(j)
                }
            }
            let index = utils.getOneFromArray(list, random)
            petItem.pet.attriQualitys2[index] += 1
        }
        return { err: 0 }
    }

    @localApi("normalRoute")
    petStarLvlup(bagIndex: number, altarIndex: number, battlePos: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos])
        let petItem: ItemModel
        if (!utils.isNull(bagIndex)) {
            petItem = player.petBag.items[bagIndex]
        } else if (!utils.isNull(altarIndex)) {
            petItem = player.altarPets.petsInAltar[altarIndex]
        } else {
            petItem = partner.pet
        }
        if (!petItem) {
            return { err: 100 }
        }
        let petConfig = gameConfigHandle.gameConfig.pet;
        let petItemConfig = gameConfigHandle.configByID[petItem.tid]
        let { totalStar1, totalStar2, totalStar } = petHandle.getTotalStar(petItem)
        if (totalStar2 >= utils.getIncValues(petConfig.petStarLvlMax, petItem.pet.quality)) {
            return { err: 2 }
        }
        let needConfig = gameConfigHandle.gameConfig.need.petStarLvlup
        let res = gameUtils.useNeedResource(player, needConfig, totalStar2)
        if (res !== 0) {
            return { err: 1 }
        }

        let list = []
        for (let j = 0; j < petItemConfig.attris.length; j++) {
            // if (!petItem.pet.attriQualitys2[j] || petItem.pet.attriQualitys2[j] < 10) {
            if (!petItem.pet.attriQualitys2[j] || petItem.pet.attriQualitys2[j] < petConfig.starMax) {
                list.push(j)
            }
        }
        let index = utils.getOneFromArray(list, random)
        if (!petItem.pet.attriQualitys2[index]) {
            petItem.pet.attriQualitys2[index] = 0
        }
        petItem.pet.attriQualitys2[index] += 1
        return { err: 0 }
    }

    @localApi("normalRoute")
    petLvlup(bagIndex: number, altarIndex: number, battlePos: number, gameRouteData?: GameRouteData) {
        let { player, isMonthVip, isYearVip } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos])
        let item: ItemModel
        if (!utils.isNull(bagIndex)) {
            item = player.petBag.items[bagIndex]
        } else if (!utils.isNull(altarIndex)) {
            item = player.altarPets.petsInAltar[altarIndex]
        } else {
            item = partner.pet
        }
        if (!item) {
            return { err: 100 }
        }
        let petAttriConfig = gameConfigHandle.gameConfig.pet
        let plvl = player.altar.lvl
        if (item.pet.lvl >= plvl) {
            return { err: 3 }
        }
        if (item.pet.lvl >= petAttriConfig.maxLvl) {
            return { err: 2 }
        }
        let needConfig = gameConfigHandle.gameConfig.need.petLvlUpNeed
        let err = gameUtils.useNeedResource(player, needConfig, item.pet.lvl)
        if (err !== 0) {
            return { err: 1 }
        }
        item.pet.lvl += 1
        playerHandle.initTeamFighterAttri(player, isMonthVip, isYearVip)
        return { err: 0 }
    }
    @localApi("normalRoute")
    petLvlup2(bagIndex: number, altarIndex: number, battlePos: number, amount: number, gameRouteData?: GameRouteData) {
        let { player, isMonthVip, isYearVip } = gameRouteData
        let partner = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos])
        let item: ItemModel
        if (!utils.isNull(bagIndex)) {
            item = player.petBag.items[bagIndex]
        } else if (!utils.isNull(altarIndex)) {
            item = player.altarPets.petsInAltar[altarIndex]
        } else {
            item = partner.pet
        }
        if (!item) {
            return { err: 100 }
        }
        let petAttriConfig = gameConfigHandle.gameConfig.pet
        let plvl = player.altar.lvl
        if (item.pet.lvl >= plvl) {
            return { err: 3 }
        }
        if (item.pet.lvl >= petAttriConfig.maxLvl) {
            return { err: 2 }
        }
        let needConfig = gameConfigHandle.gameConfig.need.petLvlUpNeed
        let upAmount = 0
        let err = gameUtils.useNeedResource(player, needConfig, item.pet.lvl)
        if (err !== 0) {
            return { err: 1 }
        }
        item.pet.lvl += 1
        upAmount++
        for (let i = 0; i < amount - 1; i++) {
            if (item.pet.lvl >= petAttriConfig.maxLvl || item.pet.lvl >= plvl) {
                break
            }
            let res = gameUtils.getNeedResource(player, needConfig, item.pet.lvl).err
            if (res !== 0) {
                break
            }
            gameUtils.useNeedResource(player, needConfig, item.pet.lvl)
            item.pet.lvl += 1
            upAmount++
        }
        playerHandle.initTeamFighterAttri(player, isMonthVip, isYearVip)
        return { err: 0, data: upAmount }
    }

    @localApi("normalRoute")
    petSync(selectPets: number[], gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        for (let i = 0; i < 3; i++) {
            let index = selectPets[i]
            if (index == null || index == undefined || !player.petBag.items[index]) {
                return { err: 90 }
            }
        }
        if (selectPets[0] == selectPets[1] || selectPets[0] == selectPets[2] || selectPets[1] == selectPets[2]) {
            return { err: 91 }
        }
        let mainPet = player.petBag.items[selectPets[0]]
        let petAttriConfig = gameConfigHandle.gameConfig.pet
        if (mainPet.pet.quality >= petAttriConfig.petQualityMax) {
            return { err: 92 }
        }
        let subPet1 = player.petBag.items[selectPets[1]]
        let subPet2 = player.petBag.items[selectPets[2]]
        if (subPet1.tid !== mainPet.tid || subPet2.tid !== mainPet.tid) {
            return { err: 93 }
        }
        if (subPet1.pet.quality !== mainPet.pet.quality || subPet2.pet.quality !== mainPet.pet.quality) {
            return { err: 94 }
        }
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.petSyncNeed, mainPet.pet.quality + 1)
        if (res !== 0) {
            return { err: 1 }
        }
        mainPet.pet.quality += 1
        bagHandle.removeItemByPosition(player.petBag, selectPets[1])
        bagHandle.removeItemByPosition(player.petBag, selectPets[2])
        return { err: 0 }
    }

    @localApi("normalRoute")
    petInherit(selectPets: number[], inheritLvl: boolean, inheritQuality: boolean, inheritStar1: boolean, inheritStar2: boolean, inheritElement: boolean, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        for (let i = 0; i < 2; i++) {
            let index = selectPets[i]
            if (index == null || index == undefined || !player.petBag.items[index]) {
                return { err: 100 }
            }
        }
        if (selectPets[0] == selectPets[1]) {
            return { err: 101 }
        }
        let mainPet = player.petBag.items[selectPets[0]]
        let petAttriConfig = gameConfigHandle.gameConfig.pet

        let subPet1 = player.petBag.items[selectPets[1]]
        if (subPet1.tid !== mainPet.tid) {
            return { err: 103 }
        }

        let needConfig = gameConfigHandle.gameConfig.need
        let inheritNeedLvl = petHandle.getInheritNeedLvl(player, selectPets, inheritLvl, inheritQuality, inheritStar1, inheritStar2, inheritElement)
        let res = gameUtils.useNeedResource(player, needConfig.petInheritNeed, inheritNeedLvl)
        if (res !== 0) {
            return { err: 1 }
        }
        if (inheritLvl) {
            mainPet.pet.lvl = subPet1.pet.lvl
        }
        if (inheritQuality) {
            mainPet.pet.quality = subPet1.pet.quality
        }
        if (inheritStar1) {
            mainPet.pet.attriQualitys1 = subPet1.pet.attriQualitys1.slice(0)
        }
        if (inheritStar2) {
            mainPet.pet.attriQualitys2 = subPet1.pet.attriQualitys2.slice(0)
        }
        if (inheritElement) {
            mainPet.pet.elements = subPet1.pet.elements.slice(0)
        }
        bagHandle.removeItemByPosition(player.petBag, selectPets[1])
        return { err: 0 }
    }

    @localApi("normalRoute")
    putAltar(bagIndex: number, gameRouteData?: GameRouteData) {
        // let { player } = gameRouteData
        // let item = player.petBag.items[bagIndex]
        // if (!item) {
        //     return { err: 100 }
        // }
        // let petAttriConfig = gameConfigHandle.gameConfig.pet
        // let altarAmount = player.altarPets.altarAddAmount + petAttriConfig.initAltarAmount
        // let index = -1
        // for (let i = 0; i < altarAmount; i++) {
        //     if (!player.altarPets.altarList[i] && index == -1) {
        //         index = i
        //     }
        //     if (player.altarPets.altarList[i] && player.altarPets.altarList[i].tid == item.tid) {
        //         return { err: 2 }
        //     }
        // }
        // if (index == -1) {
        //     return { err: 1 }
        // }
        // player.altarPets.altarList[index] = item
        // bagHandle.removeItemByPosition(player.petBag, bagIndex)
        // playerHandle.initTeamFighterAttri(player)
        // return {
        //     err: 0
        // }
    }

    @localApi("normalRoute")
    putOnPetByIndex(battlePos: number, altarIndex: number, bagIndex: number, gameRouteData?: GameRouteData) {
        let { player, isMonthVip, isYearVip } = gameRouteData
        let item = player.petBag.items[bagIndex]
        if (!item) {
            return { err: 100 }
        }
        let petAttriConfig = gameConfigHandle.gameConfig.pet
        let altarAmount = player.altarPets.altarAddAmount + petAttriConfig.initAltarAmount
        // let pets = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos]).pets
        let petsInAltar = player.altarPets.petsInAltar
        for (let i = 0; i < altarAmount; i++) {
            if (petsInAltar[i] && petsInAltar[i].tid == item.tid) {
                return { err: 2 }
            }
        }
        let originPet = player.altarPets.petsInAltar[altarIndex]
        player.altarPets.petsInAltar[altarIndex] = item
        bagHandle.removeItemByPosition(player.petBag, bagIndex)
        if (originPet) {
            bagHandle.addItemComponent(player.petBag, originPet)
        }
        playerHandle.initTeamFighterAttri(player, isMonthVip, isYearVip)
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    putOutAltar(battlePos: number, altarIndex: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        // let pets = playerHandle.getPartnerByIndexID(player, player.team.teamList[battlePos]).pets
        let item = player.altarPets.petsInAltar[altarIndex]
        if (!item) {
            return { err: 100 }
        }
        if (!bagHandle.isHasEmpty(player.petBag)) {
            return { err: 1 }
        }
        player.altarPets.petsInAltar[altarIndex] = null
        bagHandle.addItemComponent(player.petBag, item)
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    petCall(petID, gameRouteData?: GameRouteData) {
        let { player, time, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        if (!bagHandle.isHasEmpty(player.petBag)) {
            return { err: 2 }
        }
        let err = gameUtils.useNeedResource(player, needConfig.petCall, 1)
        if (err !== 0) {
            return { err: 1 }
        }
        let item = petHandle.create(petID, 35, random, { quality: 0 })
        bagHandle.addItemComponent(player.petBag, item)
        return { err: 0, data: item }
    }

    @localApi("normalRoute")
    magicMachineLvlUp(magicMachineID, gameRouteData?: GameRouteData) {
        // let { player, time, random, isMonthVip } = gameRouteData
        // let magicMachineConfig = gameConfigHandle.gameConfig.magicMachine
        // let lvl = player.magicMachine.listLvl[magicMachineID] || 0
        // if (lvl >= magicMachineConfig.maxLvl) {
        //     return { err: 2 }
        // }
        // let freeMaxTimes = adventureHandle.getMagiceMachineFreeTimes(player, isMonthVip)
        // if (player.magicMachine.freeTimes < freeMaxTimes) {
        //     player.magicMachine.freeTimes += 1
        // } else {
        //     let needConfig = gameConfigHandle.gameConfig.need.magicMachine[magicMachineID]
        //     let err = gameUtils.useNeedResource(player, needConfig, lvl)
        //     if (err !== 0) {
        //         return { err: 1 }
        //     }
        // }

        // player.magicMachine.listLvl[magicMachineID] = lvl + 1
        return { err: 0 }
    }
    @localApi("normalRoute")
    packageAttributesLvlUp(id, gameRouteData?: GameRouteData) {
        let { player, time, random, isMonthVip } = gameRouteData
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let lvl = player.packageAttributesLvls[id] || 1
        if (lvl >= equipmentConfig.packageAttributesLvlMax) {
            return { err: 2 }
        }
        let needConfig = gameConfigHandle.gameConfig.need.packageAttributes[id]
        let err = gameUtils.useNeedResource(player, needConfig, lvl)
        if (err !== 0) {
            return { err: 1 }
        }
        player.packageAttributesLvls[id] = lvl + 1
        equipmentHandle.calcEquipmentAddGoldPer(player)
        return { err: 0 }
    }

    @localApi("normalRoute")
    adjustTeam(pos1: number, pos2: number, type: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let amount1 = 0, amount2 = 0
        let teamList = player.team.teamList
        if (type == 1) {
            teamList = player.team.frontTeamList
        }
        for (let i = 0; i < 5; i++) {
            if (teamList[i]) {
                amount1 += 1
            }
        }
        for (let i = 5; i < 10; i++) {
            if (teamList[i]) {
                amount2 += 1
            }
        }
        if (pos2 > 4 && pos1 <= 4 && amount2 >= 4) {
            return { err: 1 }
        }
        if (pos1 > 4 && pos2 <= 4 && amount1 >= 4) {
            return { err: 1 }
        }
        let partner1 = teamList[pos1]
        let partner2 = teamList[pos2]
        teamList[pos1] = partner2
        teamList[pos2] = partner1
        let res = playerHandle.isMustAdjustTeam(player, type == 1)
        if (res) {
            return { err: 1 }
        }
        return { err: 0 }
    }

    @localApi("normalRoute")
    recruit(tid: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let recruitConfig = gameConfigHandle.gameConfig.recruit
        let amount = 0
        let index = -1
        let emptyList = [2, 1, 3, 7, 6, 8, 0, 4, 5, 9]
        let emptyList2 = [7, 6, 8, 5, 9]
        let occTypes = [0, 0, 0, 0, 0, 0, 0, 0]
        let amount1 = 0, amount2 = 0
        emptyList.forEach((value) => {
            let heroTid = player.team.teamList[value]
            if (heroTid) {
                let heroConfig = gameConfigHandle.configByID[heroTid]
                occTypes[heroConfig.occType] += 1
                amount += 1
                if (value <= 4) {
                    amount1 += 1
                } else {
                    amount2 += 1
                }
            } else {
                if (index == -1) {
                    index = value
                }
            }
        })
        if (amount1 >= 4) {
            for (let i = 0; i < 5; i++) {
                let pos = emptyList2[i]
                let heroTid = player.team.teamList[pos]
                if (!heroTid) {
                    index = pos
                    break
                }
            }
        }

        if (amount >= recruitConfig.recruitAmountMax) {
            return { err: 1 }
        }
        let newHeroConfig = gameConfigHandle.configByID[tid]
        if (occTypes[newHeroConfig.occType] + 1 > 2) {
            return { err: 2 }
        }
        if (index <= 4) {

        }
        let partner = player.team.recruitList[tid]
        if (!partner) {
            partner = new PartnerModel()
            partner.tid = tid
            partner.indexID = player.team.partnerIndexID++
            player.team.recruitList[tid] = partner
        }
        player.team.teamList[index] = tid

        // partner = player.team.frontRecruitList[tid]
        // if (!partner) {
        //     partner = new PartnerModel()
        //     partner.tid = tid
        //     partner.indexID = player.team.partnerIndexID++
        //     player.team.frontRecruitList[tid] = partner
        // }
        // player.team.frontTeamList[index] = tid

        return { err: 0 }
    }
    @localApi("normalRoute")
    replaceHero(index: number, tid: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let oldtid = player.team.teamList[index]
        if (!oldtid) {
            return { err: 50 }
        }
        if (playerHandle.isMustAdjustTeam(player, false)) {
            return { err: 3 }
        }
        let occTypes = [0, 0, 0, 0, 0, 0, 0, 0]
        for (let i = 0; i < player.team.teamList.length; i++) {
            let heroTid = player.team.teamList[i]

            if (heroTid) {
                if (heroTid == tid) {
                    return { err: 51 }
                }
                if (index !== i) {
                    let heroConfig = gameConfigHandle.configByID[heroTid]
                    occTypes[heroConfig.occType] += 1
                }
            }
        }
        let newHeroConfig = gameConfigHandle.configByID[tid]
        if (occTypes[newHeroConfig.occType] + 1 > 2) {
            return { err: 2 }
        }
        let oldPartner: PartnerModel = player.team.recruitList[oldtid]
        let partner: PartnerModel = player.team.recruitList[tid]
        if (!partner) {
            player.team.recruitList[tid] = new PartnerModel()
            partner = player.team.recruitList[tid]
            partner.tid = tid
        }
        partner.lvl = oldPartner.lvl
        partner.equipmentsStr = new EquipmentsLvlupModel(oldPartner.equipmentsStr)
        if (oldPartner.pet) {
            partner.pet = oldPartner.pet
            oldPartner.pet = null
        }
        oldPartner.equipmentsStr = new EquipmentsLvlupModel()
        oldPartner.lvl = 1
        player.team.teamList[index] = tid

        return { err: 0 }
    }
    @localApi("normalRoute")
    replaceHeroInFront(index: number, tid: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let oldtid = player.team.frontTeamList[index]
        if (!oldtid) {
            return { err: 50 }
        }
        if (playerHandle.isMustAdjustTeam(player, true)) {
            return { err: 3 }
        }
        let occTypes = [0, 0, 0, 0, 0, 0, 0, 0]
        for (let i = 0; i < player.team.frontTeamList.length; i++) {
            let heroTid = player.team.frontTeamList[i]

            if (heroTid) {
                if (heroTid == tid) {
                    return { err: 51 }
                }
                if (index !== i) {
                    let heroConfig = gameConfigHandle.configByID[heroTid]
                    occTypes[heroConfig.occType] += 1
                }
            }
        }
        let newHeroConfig = gameConfigHandle.configByID[tid]
        if (occTypes[newHeroConfig.occType] + 1 > 2) {
            return { err: 2 }
        }
        let oldPartner: PartnerModel = player.team.frontRecruitList[oldtid]
        let partner: PartnerModel = player.team.frontRecruitList[tid]
        if (!partner) {
            player.team.frontRecruitList[tid] = new PartnerModel()
            partner = player.team.frontRecruitList[tid]
            partner.tid = tid
        }
        partner.lvl = oldPartner.lvl
        partner.equipmentsStr = new EquipmentsLvlupModel(oldPartner.equipmentsStr)
        partner.equipmentsEnchants = new EquipmentsEnchantsModel(oldPartner.equipmentsEnchants)
        if (oldPartner.pet) {
            partner.pet = oldPartner.pet
            oldPartner.pet = null
        }
        oldPartner.equipmentsStr = new EquipmentsLvlupModel()
        oldPartner.equipmentsEnchants = new EquipmentsEnchantsModel()
        oldPartner.lvl = 1
        player.team.frontTeamList[index] = tid
        return { err: 0 }
    }

    @localApi("normalRoute")
    heroStarlvlUp(tid: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let partner: PartnerModel = player.team.recruitList[tid]
        if (!partner) {
            player.team.recruitList[tid] = new PartnerModel()
            partner = player.team.recruitList[tid]
            partner.tid = tid
        }
        if (partner.starLvl >= partnerConfig.starMaxLvl) {
            return { err: 1 }
        }
        let needConfig = gameConfigHandle.gameConfig.need.heroStarLvlUp[tid]
        let res = gameUtils.useNeedResource(player, needConfig, partner.starLvl)
        if (res !== 0) {
            return { err: 2 }
        }
        partner.starLvl += 1
        return { err: 0 }
    }

    @localApi("normalRoute")
    callChest(amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let fateConfig = gameConfigHandle.gameConfig.fate
        let times = 0, code = 0;
        let stat = {}
        for (let i = 0; i < amount; i++) {
            let rates = fateConfig.chestRate[player.chestData.callIndex]
            let rate = random.intn(100)
            if (player.itemInfo.soul < fateConfig.callChestNeedPoint) {
                code = 1
                break;
            }
            player.itemInfo.soul -= fateConfig.callChestNeedPoint
            if (rate < rates[0]) {
                player.chestData.callIndex -= 1
            } else if (rate < rates[0] + rates[1]) {
            } else if (rate < rates[0] + rates[1] + rates[2]) {
                player.chestData.callIndex += 1
            }
            times += 1
            if (player.chestData.callIndex > 4) {
                player.itemInfo['soul' + 4] += 1
                player.chestData.callIndex = 0
            } else {
                player.itemInfo['soul' + player.chestData.callIndex] += 1
            }
            player.baseInfo.dayChestTimes += 1
            player.baseInfo.openChestTimes += 1
            if (stat[player.chestData.callIndex]) {
                stat[player.chestData.callIndex] += 1
            } else {
                stat[player.chestData.callIndex] = 1
            }
        }
        return { err: 0, code, data: { times, stat } }
    }

    @localApi("normalRoute")
    openSoul(index: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let soulItem = player.itemInfo['soul' + index]
        if (!soulItem) {
            return { err: 50 }
        }
        let mapGets = [];
        let { total, maxTotal } = adventureHandle.getAdventureMapProgress(player)
        let fateConfig = gameConfigHandle.gameConfig.fate
        let chestConfig = fateConfig.chest[index]
        let adventureConfig = gameConfigHandle.gameConfig.adventure
        for (let i = 0; i < soulItem && i < 10; i++) {
            let mapGet = {}
            if (chestConfig.getSoul) {
                if (random.intn(100) < 50) {
                    if (!mapGet['soul']) {
                        mapGet['soul'] = 0
                    }
                    mapGet['soul'] += chestConfig.getSoul
                    player.itemInfo.soul += chestConfig.getSoul
                }
            }
            if (chestConfig.get) {
                chestConfig.get.forEach(item => {
                    let itemID = item.itemID
                    let values = item.incValues
                    let value = 0
                    if (adventureConfig.getResourceByFloor[itemID]) {
                        value = adventureHandle.getGoldResoureOriginAdd(player) * utils.getIncValues(values, maxTotal)
                    } else {
                        value = utils.getIncValues(values, maxTotal)
                    }
                    value = Math.floor(value)
                    if (!mapGet[itemID]) {
                        mapGet[itemID] = 0
                    }
                    mapGet[itemID] += value
                    if (typeof itemID == 'number') {
                        bagHandle.addItem(player.bag, itemID, value)
                    } else {
                        player.itemInfo[itemID] += value
                    }
                })
            }

            for (let j = 0; j < chestConfig.getAmount; j++) {
                if (chestConfig.getItems) {
                    let gets: any[] = utils.getItemsByRate(chestConfig.getItems, 1, random)
                    gets.forEach(item => {
                        let itemID = item.itemID
                        if (itemID) {
                            let values = item.incValues
                            let value = 0
                            if (adventureConfig.getResourceByFloor[itemID]) {
                                value = adventureHandle.getGoldResoureOriginAdd(player) * utils.getIncValues(values, maxTotal)
                            } else {
                                value = utils.getIncValues(values, maxTotal)
                            }

                            value = Math.floor(value)
                            if (!mapGet[itemID]) {
                                mapGet[itemID] = 0
                            }
                            mapGet[itemID] += value
                            if (typeof itemID == 'number') {
                                bagHandle.addItem(player.bag, itemID, value)
                            } else {
                                player.itemInfo[itemID] += value
                            }

                        }
                        if (item.itemIDs) {
                            let itemID: number = utils.getOneFromArray(item.itemIDs, random)
                            let values = item.incValues
                            let value = utils.getIncValues(values, maxTotal)
                            value = Math.floor(value)
                            if (!mapGet[itemID]) {
                                mapGet[itemID] = 0
                            }
                            mapGet[itemID] += value
                            bagHandle.addItem(player.bag, itemID, value)
                        }
                    })
                }
            }


            mapGets.push(mapGet)
        }

        player.itemInfo['soul' + index] = soulItem > 10 ? soulItem - 10 : 0
        return { err: 0, data: { mapGets } }
    }

    @localApi("normalRoute")
    setImmuneActive(index: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        player.research.immuneIndex = index
        return { err: 0 }
    }

    @localApi("normalRoute")
    selectOpponent(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need.arenaSelectOpponent

        let res = gameUtils.useNeedResource(player, needConfig, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        return { err: 0 }
    }
    @localApi("normalRoute")
    altarSkillLvlup(index: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let lvl = player.altar.skillsLvl[index] || 0
        let altarConfig = gameConfigHandle.gameConfig.altar
        if (lvl >= altarConfig.skillMaxLvl) {
            return { err: 50 }
        }
        let res = gameUtils.useNeedResource(player, needConfig.altarSkillLvlUp[index], lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.altar.skillsLvl[index] = lvl + 1
        return { err: 0 }
    }

    @localApi("normalRoute")
    altarUseSkill(index: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let findIndex = player.altar.skillList.findIndex((_index) => _index == index)
        if (findIndex !== -1) {
            player.altar.skillList.splice(findIndex, 1)
            return { err: 0 }
        }
        if (findIndex == -1 && player.altar.skillList.length < 2) {
            player.altar.skillList.push(index)
            return { err: 0 }
        }
        return { err: 1 }
    }

    @localApi("normalRoute")
    formationLvlup(index: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let lvl = player.formation.formationLvl[index] || 0
        let formationConfig = gameConfigHandle.gameConfig.formation
        if (lvl >= formationConfig.formationMaxLvl) {
            return { err: 50 }
        }
        let res = gameUtils.useNeedResource(player, needConfig.formationLvlUp[index], lvl)
        if (res !== 0) {
            return { err: 1 }
        }
        player.formation.formationLvl[index] = lvl + 1
        player.formation.lvl = player.formation.formationLvl.reduce((a, b) => a + b)
        return { err: 0 }
    }

    @localApi("normalRoute")
    useFormation(index: number, gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        let findIndex = player.formation.formationList.findIndex((_index) => _index == index)
        if (findIndex !== -1) {
            player.formation.formationList.splice(findIndex, 1)
            return { err: 0 }
        }
        if (findIndex == -1 && player.formation.formationList.length < 1) {
            player.formation.formationList.push(index)
            return { err: 0 }
        }
        return { err: 1 }
    }

    @localApi("normalRoute")
    lockHero(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let recruitConfig = gameConfigHandle.gameConfig.recruit
        let unLockHeroAmount = utils.keys(player.team.lockList).length
        if (player.team.lockherotid) {
            if (unLockHeroAmount !== 0) {
                let needConfig = gameConfigHandle.gameConfig.need
                let res = gameUtils.useNeedResource(player, needConfig.lockHeroNeed, 1)
                if (res !== 0) {
                    return { err: 1 }
                }
            }

        }
        let heroList = [[], [], []]
        recruitConfig.recruitList.forEach(herotid => {
            let heroItemConfig = gameConfigHandle.configByID[herotid]
            if (!player.team.lockList[herotid]) {
                heroList[heroItemConfig.quality].push(herotid)
            }
        })

        let unlockMapAmount = adventureHandle.getUnLockMapAmount(player)
        if (unLockHeroAmount >= unlockMapAmount + 1) {
            return { err: 50 }
        }

        let lockRecruitRate = adventureHandle.getHeroLockRate(player)
        let rate = random.intn(100)
        let index = 0
        if (rate <= lockRecruitRate[0]) {
            index = 0
        } else if (rate <= lockRecruitRate[0] + lockRecruitRate[1]) {
            index = 1
        } else {
            index = 2
        }
        let herotid = random.getOneFromArray(heroList[index])
        // player.team.lockList[herotid] = true
        player.team.lockherotid = herotid
        return { err: 0, data: herotid }
    }

    @localApi("normalRoute")
    confirmLockHero(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (!player.team.lockherotid) {
            return { err: 50 }
        }
        player.team.lockList[player.team.lockherotid] = true
        player.team.lockherotid = null
        return { err: 0 }
    }

    @localApi("normalRoute")
    changeTalent(talentIndex: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (player.talent.listType[talentIndex] == 1) {
            player.talent.listType[talentIndex] = 0
        } else {
            player.talent.listType[talentIndex] = 1
        }
        return { err: 0 }
    }


    @localApi("normalRoute")
    enchantEquipment(attriIndex: number, bagIndex: number, equipmentType: EquipmentType, occType: OccType, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData

        let item: ItemModel

        if (utils.isNotNull(bagIndex)) {
            item = player.equipmentBag.items[bagIndex]

        } else if (utils.isNotNull(occType)) {
            item = player.teamEquipment.equipmentList[occType][0][equipmentType]
        }

        if (!item) {
            return { err: 51 }
        }
        let attri: AttriItemModel
        let max = 0
        if (attriIndex == -1) {
            attri = item.equipment.mainAttris[0]
            let attriName = attri.name
            max = equipmentHandle.getMainValueRange(attriName, item.equipment.lvl, item.equipment.quality).max
            if (attri.value >= max) {
                return { err: 2 }
            }
        } else if (attriIndex >= 0) {
            attri = item.equipment.subAttris[attriIndex]
            let attriName = attri.name
            max = equipmentHandle.getSubValueRange(attriName, item.equipment.lvl, item.equipment.quality, attri.type).max
            if (attri.value >= max) {
                return { err: 2 }
            }
        } else {
            return { err: 50 }
        }

        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.enchantEquipment, item.equipment.quality)
        if (res !== 0) {
            return { err: 1 }
        }
        if (attri.type == AttriType.Attri) {
            if (attri.name == 'maxHp') {
                attri.value += 20
            } else {
                attri.value += 2
            }

        } else if (attri.type == AttriType.SpAttri) {
            attri.value += 0.2
        }
        if (attri.value >= max) {
            attri.value = max
        }
        item.equipment.enchant = (item.equipment.enchant || 0) + 1
        equipmentHandle.calcPower(player, item)
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    packageAttributesChange(bagIndex: number, equipmentType: EquipmentType, occType: OccType, id: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let item: ItemModel
        if (utils.isNotNull(bagIndex)) {
            item = player.equipmentBag.items[bagIndex]
        } else if (utils.isNotNull(occType)) {
            item = player.teamEquipment.equipmentList[occType][0][equipmentType]
        }
        if (!item) {
            return { err: 51 }
        }

        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.packageAttributesChange[id], 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let idCurrent = item.equipment.packageAttributesId || 0
        let getItems = []
        if (idCurrent !== 0) {
            let getId = gameConfigHandle.configByID[idCurrent].destoryGet
            bagHandle.addItem(player.bag, getId, 1)
            getItems.push({ id: getId, amount: 1 })
        }
        item.equipment.packageAttributesId = id
        equipmentHandle.calcPower(player, item)
        equipmentHandle.calcEquipmentAddGoldPer(player)
        return {
            err: 0, data: getItems
        }
    }

    @localApi("normalRoute")
    washEquipment(attriIndex: number, bagIndex: number, equipmentType: EquipmentType, occType: OccType, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let item: ItemModel
        if (utils.isNotNull(bagIndex)) {
            item = player.equipmentBag.items[bagIndex]

        } else if (utils.isNotNull(occType)) {
            item = player.teamEquipment.equipmentList[occType][0][equipmentType]
        }

        if (!item) {
            return { err: 51 }
        }
        if (utils.isNotNull(item.equipment.washIndex) && item.equipment.washIndex !== attriIndex) {
            return { err: 52 }
        }
        if (attriIndex < 0) {
            return { err: 50 }
        }
        let attri: AttriItemModel = item.equipment.subAttris[attriIndex]
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.washEquipment, item.equipment.quality)
        if (res !== 0) {
            return { err: 1 }
        }
        item.equipment.washIndex = attriIndex
        equipmentHandle.washEquipmentSubAttri(item, attriIndex, random)
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    altarStr(index, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let needConfig = gameConfigHandle.gameConfig.need
        let attriItemConfig = gameConfigHandle.gameConfig.altar.attriTypeList[index]
        if (!attriItemConfig) {
            return {
                err: 1
            }
        }
        let lock = true
        if (attriItemConfig.preIndex != null && attriItemConfig.preLvl) {
            let preIndex = attriItemConfig.preIndex
            let preLvl = player.altar.attriLvls[preIndex] || 0
            if (preLvl >= attriItemConfig.preLvl) {
                lock = false
            }
        } else if (attriItemConfig.preIndex == null || !attriItemConfig.preLvl) {
            lock = false
        }
        if (lock) {
            return {
                err: 2
            }
        }
        let altarLvl = player.altar.attriLvls[index] || 0
        if (altarLvl >= gameConfigHandle.gameConfig.altar.altarStrMaxLvl) {
            return {
                err: 3
            }
        }
        let res = gameUtils.useNeedResource(player, needConfig.altarStrNeed[index], altarLvl + 1)
        if (res !== 0) {
            return { err: 4 }
        }
        player.altar.attriLvls[index] = altarLvl + 1
        player.altar.totalAttriLvl = Object.keys(player.altar.attriLvls).reduce((a, b) => a + player.altar.attriLvls[b], 0)
        return { err: 0 }
    }

    @localApi("normalRoute")
    forgeEquipment(bagIndex: number, equipmentType: EquipmentType, occType: OccType, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let partnerIndexID = playerHandle.getFirstPartnerIndexID(player)
        let partner = playerHandle.getPartnerByIndexID(player, partnerIndexID)
        let item: ItemModel

        if (utils.isNotNull(bagIndex)) {
            item = player.equipmentBag.items[bagIndex]

        } else if (utils.isNotNull(occType)) {
            item = player.teamEquipment.equipmentList[occType][0][equipmentType]
        }

        if (!item) {
            return { err: 51 }
        }

        if (item.equipment.lvl >= player.altar.lvl) {
            return { err: 2 }
        }
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        if ((item.equipment.forceLvl || 0) >= equipmentConfig.equipmentForgeLvlUpMax * (item.equipment.quality + 1)) {
            return { err: 3 }
        }

        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.forgeEquipment, item.equipment.quality)
        item.equipment.forceLvl += 1
        item.equipment.lvl += 1
        if (res !== 0) {
            return { err: 1 }
        }
        return {
            err: 0
        }
    }
    @localApi("normalRoute")
    addDayArenaTimes(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.baseInfo.dayArenaTimes += 1
        return {
            err: 0
        }
    }

    @localApi("normalRoute")
    getProgressGift(key, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let progressGiftConfig = gameConfigHandle.gameConfig.map.mapProgressGift
        let giftConfig = progressGiftConfig.find(gc => gc.key == key)
        if (!giftConfig) {
            return { err: 1 }
        }
        let got = player.baseInfo.mapProgressGifts.includes(key)
        if (got) {
            return { err: 2 }
        }
        giftConfig.items.forEach(item => {
            if (typeof item[0] == 'string') {
                player.itemInfo[item[0]] += Number(item[1])
            } else if (typeof item[0] == 'number') {
                bagHandle.addItem(player.bag, item[0], Number(item[1]))
            }
        })
        player.baseInfo.mapProgressGifts.push(key)
        return {
            err: 0,
            data: giftConfig.items
        }
    }

    @localApi("normalRoute")
    getReverseGift(key, value, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let reverseGiftConfig = gameConfigHandle.gameConfig.map.reverseGift
        let giftConfig = reverseGiftConfig.find(gc => gc.key == key)
        if (!giftConfig) {
            return { err: 1 }
        }
        let got = player.baseInfo.reverseGifts.includes(key)
        if (got) {
            return { err: 2 }
        }
        if (giftConfig.value > value) {
            return { err: 3 }
        }
        giftConfig.items.forEach(item => {
            if (typeof item[0] == 'string') {
                player.itemInfo[item[0]] += Number(item[1])
            } else if (typeof item[0] == 'number') {
                bagHandle.addItem(player.bag, item[0], Number(item[1]))
            }
        })
        player.baseInfo.reverseGifts.push(key)
        return {
            err: 0,
            data: giftConfig.items
        }
    }

    @localApi("normalRoute")
    setArenaFormation(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.baseInfo.setArenaTimes += 1
        return {
            err: 0
        }
    }


    @localApi("normalRoute")
    equipmentEnchantUp(tid: number, type: EquipmentType, amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let code = 0
        let times = 0
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let partner = playerHandle.getPartnerByIndexID(player, tid)
        let equipmentsEnchants: EquipmentsEnchantsModel = partner.equipmentsEnchants
        if (!equipmentsEnchants[type]) {
            equipmentsEnchants[type] = new EnchantModel()
        }
        for (let i = 0; i < amount; i++) {


            if (equipmentsEnchants[type].mainLvl >= partner.lvl) {
                code = 2
                break
            }
            if (equipmentsEnchants[type].mainLvl >= 3500) {
                code = 3
                break
            }
            let needConfig = gameConfigHandle.gameConfig.need
            let err = gameUtils.useNeedResource(player, needConfig.equipmentEnchantNeed, equipmentsEnchants[type].mainLvl)
            if (err !== 0) {
                code = 1
                break
            }

            if (!equipmentsEnchants[type].mainType) {
                equipmentsEnchants[type].mainType = utils.getOneFromArray(equipmentConfig.equipmentEnchantType1[type])
            }
            equipmentsEnchants[type].mainLvl += 1;
            if (equipmentsEnchants[type].mainLvl >= 100) {
                if (!equipmentsEnchants[type].subType) {
                    equipmentsEnchants[type].subType = utils.getOneFromArray(equipmentConfig.equipmentEnchantType2[type])

                }
                equipmentsEnchants[type].subLvl = Math.floor(equipmentsEnchants[type].mainLvl / 100);

            }
            times += 1
        }

        return { err: 0, data: { code: code, times } }

    }



    @localApi("normalRoute")
    restartEnchant(tid: number, type: EquipmentType, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let code = 0
        let times = 0
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let partner = playerHandle.getPartnerByIndexID(player, tid)
        let equipmentsEnchants: EquipmentsEnchantsModel = partner.equipmentsEnchants
        if (!equipmentsEnchants[type]) {
            equipmentsEnchants[type] = new EnchantModel()
        }


        let needConfig = gameConfigHandle.gameConfig.need
        let err = gameUtils.useNeedResource(player, needConfig.refreshEquipmentEnchant, 1)
        if (err !== 0) {
            return { err: 1 }
        }
        equipmentsEnchants[type].mainType = utils.getOneFromArray(equipmentConfig.equipmentEnchantType1[type])
        if (equipmentsEnchants[type].mainLvl >= 100) {
            equipmentsEnchants[type].subType = utils.getOneFromArray(equipmentConfig.equipmentEnchantType2[type])
        }
        return { err: 0 }
    }

    @localApi("normalRoute")
    getAreaCsReward(playerCountPer: number, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData


        let areaCsConfig = gameConfigHandle.gameConfig.areaCs
        if (player.adventure.areaCsReward != 0) {
            return { err: 1 }
        }

        if (!bagHandle.isHasEmpty(player.bag)) {
            return { err: 3 }
        }

        let mapGet = {}

        areaCsConfig.getRewardAreaCs.forEach(item => {
            let itemID = item.itemID
            if (itemID) {
                let values = item.incValues
                let lvl = playerCountPer

                if (lvl >= 10) {
                    lvl = 10
                }

                let amountValue = utils.getIncValues(values, lvl)
                if (!mapGet[itemID]) {
                    mapGet[itemID] = 0
                }
                mapGet[itemID] += amountValue
                if (typeof itemID == 'string') {
                    player.itemInfo[itemID] += amountValue
                } else if (typeof itemID == 'number') {
                    bagHandle.addItem(player.bag, itemID, amountValue)
                }
            }
        })


        player.adventure.areaCsReward = 1

        return {
            err: 0,
            data: {
                mapGet,
            }
        }
    }

}

let normalRoute: NormalRoute = new NormalRoute()
export default normalRoute